hiddencaliber / flutter_multiselect

Flutter package for multi-select UI widget
Other
52 stars 37 forks source link

setState inside _onSaved returns error #29

Open fral8 opened 3 years ago

fral8 commented 3 years ago

Dear All, I've noticed an issue when I call setState inside _onSaved function. Basically, I need to set the selected values in my state.

  onSaved: (value) {
    setState(() => this.selectedData = value);
  }

This line causes the error: The following NoSuchMethodError was thrown building MultiSelect(dirty, dependencies: [_FormScope], state: FormFieldState<dynamic>#140cb): The method '[]' was called on null. Receiver: null Tried calling: []("display")

It's well known that forms are reloaded on setState function (see here), but how to solve this issue for the library?

Sincerely, Francesco

hiddencaliber commented 3 years ago

@fral8 : can u please share a reproducible example. Thanks

fral8 commented 3 years ago

Here you can find a reproducible example based on the documentation example.


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

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
        brightness:  Brightness.dark,
      ),
      home: MyHomePage(title: 'Flutter Demo - Multiselect'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  dynamic data;

  @override 
  initState() {
    super.initState();
      }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("title"),
      ),
      body: Center(
        child: new Form(
          key: _formKey,
          autovalidate: true,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: new MultiSelect(
                    autovalidate: true,

                    titleText: 'Country of Residence',

                    validator: (dynamic value) {
                      if (value == null) {
                        return 'Please select one or more option(s)';
                      }
                      return null;
                    },
                    errorText: 'Please select one or more option(s)',
                    dataSource: [
                    {"name": "Afghanistan", "code": "AF"},
                    {"name": "Wallis and Futuna", "code": "WF"},
                    {"name": "Western Sahara", "code": "EH"},
                    {"name": "Yemen", "code": "YE"},
                    {"name": "Zambia", "code": "ZM"},
                    {"name": "Zimbabwe", "code": "ZW"}
                    ],
                    textField: 'name',
                    valueField: 'code',
                    filterable: true,
                    required: true,
                    onSaved: (value) {
                      print('The value is $value');
                      //Error on line 108
                      setState(() {

                        this.data=value;
                      });
                    },

                    ),
              ),
              SizedBox(
                width: 10.0,
              ),
              RaisedButton(
                child: Text('Save'),
                color: Theme.of(context).primaryColor,
                onPressed: () {
                  _onFormSaved();
                },
              )
            ],
          ),
        ),
      ),
    );
  }

  void _onFormSaved() {
    final FormState form = _formKey.currentState;
    form.save();
    //if I call setState here, I receive same error.
  }
}