Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

onInit handler is not called on initialisation #68

Closed
kaboc opened this issue Apr 2, 2020 · 5 comments
Closed

onInit handler is not called on initialisation #68

kaboc opened this issue Apr 2, 2020 · 5 comments

Comments

@kaboc
Copy link

kaboc commented Apr 2, 2020

onInit is called from didUpdateWidget(), which is called only on rebuild, resulting in it not getting called on initialisation.

@imtoori
Copy link
Owner

imtoori commented Apr 4, 2020

I just tried the example, but it's working. It's called on initialization

@kaboc
Copy link
Author

kaboc commented Apr 4, 2020

Here is the minimum code.

import 'package:flutter/material.dart';
import 'package:country_code_picker/country_code_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('build');

    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: CountryCodePicker(
            initialSelection: 'US',
            onInit: print,
          ),
        ),
      ),
    );
  }
}

If it is true that onInit is called on initialisation, it should prints "build" and then "+1", but actually it does only when "build" is output more than once by rebuilds as the results of hot reloads/restarts or of other causes.


Additionally, would you try the following code as well please? It does not print "didUpdateWidget", which means didUpdateWidget is not called on initialisation.
As this code does not depend on third party packages, you can run it on DartPad too.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  MyApp();

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
    print('initState');
  }

  @override
  void didUpdateWidget(MyApp oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('didUpdateWidget');
  }

  @override
  Widget build(BuildContext context) {
    return const SizedBox.shrink();
  }
}

@imtoori
Copy link
Owner

imtoori commented Apr 5, 2020

I can swear that the minimum code part prints "+1"

Anyway I believe that there is a problem, I'll analyze it better

This was referenced Apr 7, 2020
@imtoori
Copy link
Owner

imtoori commented Apr 7, 2020

#74 fixes this bug

@imtoori imtoori closed this as completed Apr 7, 2020
@kaboc
Copy link
Author

kaboc commented Apr 7, 2020

Thank you! 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants