19

After closing the app, when I try to open it again, I'm getting the following error but it's only on iOS platform, Android works well.

enter image description here

I have looked around and there are several SO questions and issues about this problem but I couldn't solve it. I'm also using bloc pattern for managing state.

I have GlobalKey<FormState> in my AuthenticateForm.

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:low_code/blocs/authentication/authentication_bloc.dart';
import 'package:low_code/blocs/authentication/authentication_event.dart';
import 'package:low_code/helpers/app_localization/app_localizations.dart';

class AuthenticateForm extends StatefulWidget {
  @override
  _AuthenticateFormState createState() => _AuthenticateFormState();
}

class _AuthenticateFormState extends State<AuthenticateForm> {
  GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  String username;

  String password;

  @override
  Widget build(BuildContext context) {
    AppLocalizations appLocalizations = AppLocalizations.of(context);
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          TextFormField(
            onSaved: (String value) => username = value,
            initialValue: 'dms-bpm',
            decoration: InputDecoration(
                labelText: appLocalizations.translate('username')),
            // ignore: missing_return
            validator: (String value) {
              if (value.isEmpty) {
                return 'Please enter your ${appLocalizations.translate('username')}';
              }
            },
          ),
          TextFormField(
            onSaved: (String value) => password = value,
            obscureText: true,
            decoration: InputDecoration(
                labelText: appLocalizations.translate('password')),
            initialValue: 'dms-bpm',
            // ignore: missing_return
            validator: (String value) {
              if (value.isEmpty) {
                return 'Please enter your ${appLocalizations.translate('password')}';
              }
            },
          ),
          RaisedButton(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(15))),
            child: Text(appLocalizations.translate('login')),
            onPressed: () {
              _formKey.currentState.save();
              if (_formKey.currentState.validate()) {
                BlocProvider.of<AuthenticationBloc>(context)
                  ..dispatch(
                      Authenticate(username: username, password: password));
              }
            },
          )
        ],
      ),
    );
  }
}

Flutter (Channel stable, v1.7.8+hotfix.4, on Microsoft Windows [Version 10.0.17763.615], locale en-GB)

live-love
  • 48,840
  • 22
  • 240
  • 204
cipli onat
  • 1,943
  • 1
  • 11
  • 26

11 Answers11

20

Try stop and rerun you application instead of hot reloading. That solved my problem.

Leonard Zhou
  • 447
  • 1
  • 4
  • 9
  • 3
    this can't be a solution. App must need a hot reload or restart – Mukta Apr 22 '21 at 07:49
  • It does work @Mukta, try this option only once untill your problem gets resolved. Mine did – Ashenafi Chufamo Dec 26 '22 at 08:40
  • Still you don't have any idea about Global Key of Flutter. Notice the question. He is using a single key for multiple Widget or Screen. Either he needs to use a list of Global keys or different naming single key. Still now I am disagree with your answer. Sorry – Mukta Jan 08 '23 at 05:27
13

In my case this error happened because I had a initialRoute property set, while instead I should set the home property:

class RouteTestApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      //initialRoute: '/', //remove this
      home: FirstScreen(),  //this is the calling screen
      routes: {
        ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(),
      },
    );
  }
}
live-love
  • 48,840
  • 22
  • 240
  • 204
4

i just stopped the hot reload and restarted the app it worked for me

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29834684) – All Downhill From Here Sep 15 '21 at 21:32
1

In my case, I run the flutter app without adding home: BottomNavBar(). after getting an error I added home: BottomNavBar() and give hot reload (Command + s) which reproduce this error (A global key was used multiple times inside one widget child list). Then I just reconnect my target device and run again, which fixed my issue.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        brightness:Brightness.light,
        primaryColor: Colors.white,
        accentColor: Colors.blue,
        fontFamily: 'Poppins',
        appBarTheme: AppBarTheme(
          centerTitle: true,
        ),
      ),
      home: BottomNavBar(),
    );
  }
}
Rezaul Islam
  • 603
  • 9
  • 12
1

In case you are getting the error when the app just started: Add '/' route to the list of routes and restart.

AbdKa
  • 156
  • 6
0

Try to make your GlobalKey "final". I think the fact your global key is mutable might be creating this problem.

0

just kill the app run (stop) and uninstall it then reinstall it again to reset the navigator state

Hisham Shami
  • 439
  • 4
  • 8
0

Just assign UniqueKey() to your ListView.

0

in my case i was forgot add async await for main()

main() async {
  await initModul();
  runApp(MyApp());
}

initModul() async {
  WidgetsFlutterBinding.ensureInitialized();

  final SharedPreferences sharedPreferences =
      await SharedPreferences.getInstance();

  instance.registerLazySingleton<SharedPreferences>(
    () => sharedPreferences,
  );

  instance.registerLazySingleton<AppSettingsSharedPreferences>(
      () => AppSettingsSharedPreferences(instance()));

  // for clear preferences:
  // AppSettingsSharedPreferences appSettingsSharedPreferences =
  // instance<AppSettingsSharedPreferences>();
  // appSettingsSharedPreferences.clear();
}
0

You can Try stop and rerun you application instead of hot reloading. That solved my problem. enter image description here

-1

In my case, the calling screen in the home: property of the MaterialApp() object was returning null. After fixing it, I stopped the app and reran it.

Macdonald
  • 880
  • 7
  • 21