0

I actually have two questions.

  1. I'm trying to add a String type object to an existing array in Firestore like this:
  addWalletToUser(coinAddress) async {
    final FirebaseUser user = (await FirebaseAuth.instance.currentUser());
    try {
      await Firestore.instance.collection('users').document(user.uid)
          .updateData({
            "cryptoAddresses": FieldValue.arrayUnion(coinAddress)
          });
    } catch (e) {
      print('error caught: $e');
      return null;
    }
  }

This results in the following error: flutter: type 'String' is not a subtype of type 'List<dynamic>'

  1. My second question is why my code in this if-statement is carried out while addWalletToUser should return null:
if (addWalletToUser(_textController.text) != null)

Any help will be greatly appreciated!

2
  • 1
    addWalletToUser function is async so it returns a Future which is not null. What you are returning inside the async function is obtained as await addWalletToUser(text) which will now give null. Also what is the function FieldValue.arrayUnion returning? Commented Dec 5, 2019 at 11:10
  • Please limit yourself to a single question per post, so that it can be answered with a single answer. See meta.stackexchange.com/questions/39223/… Commented Dec 5, 2019 at 14:44

2 Answers 2

5

Regarding this error:

flutter: type 'String' is not a subtype of type 'List'

This means you are trying to do assign a String to a List (as in list = string).

The arrayUnion takes a parameter of type List<dynamic>:

  static FieldValue arrayUnion(List<dynamic> elements) =>
      FieldValue._(FieldValueType.arrayUnion, elements);

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/field_value.dart#L35

Therefore you need to do the following:

"cryptoAddresses": FieldValue.arrayUnion(['data1','data2','data3'])

In your case, it's probably going to be:

"cryptoAddresses": FieldValue.arrayUnion([coinAddress])
Sign up to request clarification or add additional context in comments.

Comments

0

I solved the second problem by creating a third function:

  Future storeValues(coinAddress) async {
    final walletToUser = await addWalletToUser(coinAddress);
    final wallet = await createWallet(coinAddress);
    return [walletToUser,wallet];
  }

and by calling that function with .then() like so:

 storeValues(_textController.text).then((val) {
                      if (val[0] != null && val[1] != null) {
                      //do something here
}}

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.