Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Isolate] Null check operator used on a null value #22

Open
bhanuka96 opened this issue Sep 6, 2021 · 3 comments
Open

[Isolate] Null check operator used on a null value #22

bhanuka96 opened this issue Sep 6, 2021 · 3 comments

Comments

@bhanuka96
Copy link

I tried to implement isolate to update firestore database. But I got Null check operator error. Can't firebase use on secondary isolate?

Error

I/flutter ( 9135): [Null check operator used on a null value, #0      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
I/flutter ( 9135): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
I/flutter ( 9135): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
I/flutter ( 9135): #3      MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:344:41)
I/flutter ( 9135): #4      MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:30:37)
I/flutter ( 9135): #5      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:77:13)
I/flutter ( 9135): #6      Firebase.initializeApp (package:firebase_core/src/firebase.dart:42:47)
I/flutter ( 9135): #7      Worker.readUpdate (package:yello/services/chatMessageService.dart:202:20)
I/flutter ( 9135): #8      Worker._isolateEntry.<anonymous closure> (package:yello/services/chatMessageService.dart:214:15)
I/flutter ( 9135): #9      Worker._isolateEntry.<anon
I/chatty  ( 9135): uid=10206(u0_a206) 1.ui identical 1 line
I/flutter ( 9135): [Null check operator used on a null value, #0      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
I/flutter ( 9135): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
I/flutter ( 9135): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
I/flutter ( 9135): #3      MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:344:41)
I/flutter ( 9135): #4      MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:30:37)
I/flutter ( 9135): #5      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:77:13)
I/flutter ( 9135): #6      Firebase.initializeApp (package:firebase_core/src/firebase.dart:42:47)
I/flutter ( 9135): #7      Worker.readUpdate (package:yello/services/chatMessageService.dart:202:20)
I/flutter ( 9135): #8      Worker._isolateEntry.<anonymous closure> (package:yello/services/chatMessageService.dart:214:15)
I/flutter ( 9135): #9      Worker._isolateEntry.<anon

Full Code

class Worker {
  SendPort _sendPort;

  Isolate _isolate;

  final _isolateReady = Completer<void>();

  Worker() {
    init();
  }

  Future<void> get isReady => _isolateReady.future;

  void dispose() {
    _isolate.kill();
  }

  Future<void> fetch(ChatModel model) async {
    _sendPort.send(model);
  }

  Future<void> init() async {
    final receivePort = ReceivePort();
    final errorPort = ReceivePort();
    errorPort.listen(print);

    receivePort.listen(_handleMessage);
    _isolate = await Isolate.spawn(
      _isolateEntry,
      receivePort.sendPort,
      onError: errorPort.sendPort,
    );
  }

  void _handleMessage(dynamic message) {
    if (message is SendPort) {
      _sendPort = message;
      _isolateReady.complete();
      return;
    }

    if (message is bool) {
      return;
    }

    throw UnimplementedError("Undefined behavior for message: $message");
  }

  static Future<void> readUpdate(ChatModel model) async {
    await Firebase.initializeApp();
    final FirebaseFirestore _db = FirebaseFirestore.instance;
    return _db.collection('chats').doc(model.chatFieldId).collection('messages').doc(model.messageId).update({'readStatus': 'R'});
  }

  static void _isolateEntry(dynamic message) {
    SendPort sendPort;
    final receivePort = ReceivePort();

    receivePort.listen((dynamic message) async {
      assert(message is ChatModel);
      try {
        await readUpdate(message);
        sendPort.send(true);
      } finally {
        //client.close();
      }
    });

    if (message is SendPort) {
      sendPort = message;
      sendPort.send(receivePort.sendPort);
      return;
    }
  }
}
@BernardinD
Copy link

Similar issue with me. Any progress?

@dsemama
Copy link

dsemama commented Feb 9, 2022

Same here.

@yadavkhushwant
Copy link

I am facing the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants