0

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});
    [...]
  }
4
  • 1
    Try using await >> await DatabaseService(uid: userData.uid) .addListToDatabase(newList); Commented Jun 8, 2020 at 6:29
  • 1
    It worked with async await! Thank you so much! 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. What I really don't understand is that it was working before... I just worked changing the design in the past 2 days, and somehow it changed there. Probably I've changed the line order but I don't remember. Commented Jun 8, 2020 at 14:39
  • Same happens to me. When the list.clear() is called, the list that you pass also cleared. Thank you for that tip @HyungTaeCarapetoFigur Commented May 11, 2021 at 12:14
  • You can also check the answer here stackoverflow.com/questions/58389591/… Commented May 11, 2021 at 12:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.