Hyung Tae Carapeto Figur
Hyung Tae Carapeto Figur

Reputation: 1533

Dart/Flutter: Strings of element inside a List becomes empty when passing as an argument (Why??)

Strings of element inside a List becomes empty when passing as an argument. It was working before. I don't know what happened that it stopped working, and started passing empty.

I have a model called SubjectiveList, it is the list I am talking about.

class SubjectiveList {
  String id;
  String name;
  List<Item> items;
  SubjectiveList({this.id, this.name, this.items});
}

This list has the property items. What becomes empty is the properties inside the Item object.

class Item {
  String id;
  String name;
  Content content;

  Item({this.id, this.name, this.content});
}

On the debugger, The newList instance appears fine, with the object names (ps: the ID is okay to be null at this point because it will come from Firestore Database later)

Here is the code with the screenshots: Screenshot before passing list as argument

Future<dynamic> showListInfoDialog() {
    final userData = Provider.of<UserData>(context, listen: false);
    GlobalKey<FormState> _addListInfoFormKey = GlobalKey<FormState>();
    final ValueNotifier<int> tabIndex =
        Provider.of<ValueNotifier<int>>(context, listen: false);
    TempListViewModel tempList =
        Provider.of<TempListViewModel>(context, listen: false);

    return showDialog(
      context: context,
      child: SimpleDialog(
        title: Text("List Info"),
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(defaultSpacing),
            child: Form(
              key: _addListInfoFormKey,
              child: Column(
                children: <Widget>[
                  TextFormField(
                    onChanged: (val) => tempList.setListName(val),
                    validator: (val) => val.isEmpty ? 'Write a name' : null,
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.featured_play_list),
                      labelText: "List Name",
                    ),
                  ),
                  SizedBox(height: defaultSpacing),
                  SizedBox(
                    width: double.infinity,
                    child: RaisedButton(
                      child: Text("Create List"),
                      color: successColor,
                      onPressed: () {
                        if (_addListInfoFormKey.currentState.validate()) {
                          final newList = SubjectiveList(
                              name: tempList.list.name,
                              items: tempList.list.items);
                          DatabaseService(uid: userData.uid)
                              .addListToDatabase(newList); // <-- HERE
                          tempList.init();
                          tabIndex.value = 0;
                          Navigator.of(context).pop();
                        }
                      },
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

And then it appears empty when coming to the function!!

enter image description here

Future addListToDatabase(SubjectiveList list) async { <-- HERE
    DocumentReference listDocument =
        await userDocument.collection('lists').add({'name': list.name});
    [...]
  }

Upvotes: 0

Views: 684

Answers (1)

Hyung Tae Carapeto Figur
Hyung Tae Carapeto Figur

Reputation: 1533

Thanks @edenar Now I understand what happened. In Flutter the line "final newList = SubjectiveList(name: tempList.list.name, items: tempList.list.items);" makes a pointer reference, and not an declaration of the current value. So, when it goes to the next line and executes tempList.init() it is clearing the list before getting the argument in the function.

So it worked putting await in that line.

Upvotes: 1

Related Questions