Tanvir Arafat
Tanvir Arafat

Reputation: 41

Invalid argument(s): Illegal argument in isolate message in flutter

I am trying to use flutter isolate,But it gives me error as,I am calling APIs from back end and passing it to my isolate function using compute: [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Invalid argument(s): Illegal argument in isolate message : (object is a closure - Function '': static.) Can anyone help me to solve the issue?

class PhoneContactList extends StatefulWidget {
  @override
  _PhoneContactListState createState() => _PhoneContactListState();
}

class _PhoneContactListState extends State<PhoneContactList> {
  //ContractSyncRX contractSyncRX = ContractSyncRX();

  void initState() {
    getContacts();
    super.initState();
  }


  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          CustomAppBar(
            color: CustomColor.themeColor,
            leftBackButton: true,
            leftBackButtonColor: Colors.white,
            titleText: "Contacts",
            titleTextColor: Colors.white,
            leftBackButtonFunction: () {
              pop(context: context);
            },
          ),
          Expanded(
            child: FutureBuilder<Iterable<Contact>>(
              future: getContacts(),
              builder: (context, snapshot) {
                return snapshot.hasData
                    ? contractListView(snapshot.data)
                    : loadingIndicatorCircle(
                        context: context,
                        color: CustomColor.themeColor,
                        size: bCon.hbSize(10),
                      );
              },
            ),
          )
        ],
      ),
    );
  }

  ListView contractListView(Iterable<Contact> contacts) {
    return ListView(
      children: <Widget>[
        ...contacts.map(
          (c) {
            return c.phones.length > 0
                ? Container(
                    padding: EdgeInsets.symmetric(horizontal: bCon.hbSize(5)),
                    child: Column(
                      children: <Widget>[
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: <Widget>[
                                Container(
                                  margin: EdgeInsets.only(
                                    right: bCon.hbSize(2),
                                  ),
                                  width: bCon.hbSize(22),
                                  height: bCon.hbSize(22),
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(
                                      Radius.circular(CustomRadius.fullRadius),
                                    ),
                                    image: Platform.isIOS
                                        ? DecorationImage(
                                            image: c.avatar != null
                                                ? MemoryImage(c.avatar)
                                                : AssetImage(
                                                    CustomImages.noImage),
                                            fit: BoxFit.cover,
                                          )
                                        : DecorationImage(
                                            image: c.avatar.isNotEmpty
                                                ? MemoryImage(c.avatar)
                                                : AssetImage(
                                                    CustomImages.noImage),
                                            fit: BoxFit.cover,
                                          ),
                                  ),
                                ),
                                //
                                SizedBox(
                                  width: bCon.hbSize(3),
                                ),
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    CustomText(
                                      text: c.displayName,
                                      fontSize: sCon.scaledSize(14),
                                    ),
                                    SizedBox(
                                      height: bCon.hbSize(2),
                                    ),
                                    CustomText(
                                      text: c.phones.length > 0
                                          ? c.phones.first.value
                                          : "No number",
                                      fontSize: sCon.scaledSize(14),
                                    ),
                                    UdGapY(
                                      value: UdDesign.pixels(8),
                                    ),
                                    UdBasicButton(
                                      height: UdDesign.pixels(30),
                                      width: UdDesign.pixels(119),
                                      borderRadius: UdDesign.pixels(15),
                                      titleFontSize: UdDesign.fontSize(14),
                                      titleFontWeight: FontWeight.w400,
                                      title: "Refer Now",
                                      backgroundColor: CustomColor.themeColor,
                                    )
                                  ],
                                )
                              ],
                            ),

                            //
                          ],
                        ),
                        SizedBox(
                          height: bCon.hbSize(8),
                        ),
                      ],
                    ),
                  )
                : SizedBox.shrink();
          },
        )
      ],
    );
  }
}

Future<Iterable<Contact>> getContacts() async {
  Iterable<Contact> contacts = await ContactsService.getContacts();
  compute(syncContracts, contacts);
  return contacts;
}

syncContracts(Iterable<Contact> retrivedContacts) async {
  ContractSyncRX contractSyncRX = ContractSyncRX();
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var contractID = prefs.getString(PreferenceKey.contactId);
  retrivedContacts.forEach((element) {
    Map<String, dynamic> contractPayload;
    contractPayload = {
      "contactId": contractID,
      "contacts": [
        {
          "firstName": element.displayName,
          "lastName": null,
          "mobile": element.phones.isNotEmpty
              ? element.phones.elementAt(0).value
              : null,
          "email": element.emails.isNotEmpty
              ? element.emails.elementAt(0).value
              : null,
        }
      ],
    };
    contractSyncRX.syncContractData(contractPayload);
  });
  contractSyncRX.clean();
  contractSyncRX.dispose();
}

Upvotes: 3

Views: 6280

Answers (1)

Lucas Britto
Lucas Britto

Reputation: 375

Hey @Tanvir Arafat isolates don't share memory between threads, which means that you can only send primitive values and static/top-level functions. The iterable is not a primitive value since it's still iterating through the values, if you turn it into a List you will be able to send it to the isolate.

Check out the easy_isolate plugin, it provides an easy way to work with isolates and with a good explanation.

https://pub.dev/packages/easy_isolate

Upvotes: 2

Related Questions