Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A global key was used multiple times inside one widgets child list

Tags:

flutter

dart

bloc

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)

like image 994
cipli onat Avatar asked Aug 27 '19 11:08

cipli onat


2 Answers

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

like image 176
Collen Zhou Avatar answered Oct 16 '22 08:10

Collen Zhou


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(),
      },
    );
  }
}
like image 33
live-love Avatar answered Oct 16 '22 08:10

live-love