在Flutter中运行分离菌株;未行程的例外状况:UI操作仅适用于根分离菌株

kgsdhlau  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(183)

因为我的flutter应用程序运行不流畅,我认为问题可能是flutter中的单线程。所以我开始采取第一步运行隔离来避免一个事件循环。然而,我遇到了以下问题,这可能是错误地使用了隔离:
我从类的外部调用addNewChild方法。我将所有变量转换为map,以运行compute方法并调用addNewChildStatic方法,即static,如下所示:

void addNewChild(
    childFacilityId,
    childFacility,
    childGroup,
    groupId,
    childFirstName,
    childLastName,
    userImageFile,
    isHorizontalImage,
  ) {
    Map map = Map();
    map['childFacilityId'] = childFacilityId;
    map['childFacility'] = childFacility;
    map['childGroup'] = childGroup;
    map['groupId'] = groupId;
    map['childFirstName'] = childFirstName;
    map['childLastName'] = childLastName;
    map['userImageFile'] = userImageFile;
    map['isHorizontalImage'] = isHorizontalImage;

    compute(addNewChildStatic, map); //here I run isolate using compute method
  }

addNewChildStatic看起来像这样:

static Future<void> addNewChildStatic(Map map) async {
    print('COMPUTE addNewChildStatic STARTED');
    WidgetsFlutterBinding.ensureInitialized(); // I have added this because the errors I was getting in the console
    Firebase.initializeApp(); // I have added this because the errors I was getting in the console that there was no Firebase

    final childFacilityId = map['childFacilityId'];
    final childFacility = map['childFacility'];
    final childGroup = map['childGroup'];
    final childGroupId = map['groupId'];
    final childFirstName = map['childFirstName'];
    final childLastName = map['childLastName'];
    final childImageFile = map['userImageFile'];
    final isHorizontalImage = map['isHorizontalImage'];

try{
//methods
} catch(e){
//error handling
}
    }

问题是我得到以下错误:

flutter: COMPUTE addNewChildStatic STARTED
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: UI actions are only available on root isolate.
#0      PlatformDispatcher._nativeSetNeedsReportTimings (dart:ui/platform_dispatcher.dart:501:59)
#1      PlatformDispatcher.onReportTimings= (dart:ui/platform_dispatcher.dart:493:29)
#2      SchedulerBinding.addTimingsCallback (package:flutter/src/scheduler/binding.dart:279:26)
#3      SchedulerBinding.initInstances (package:flutter/src/scheduler/binding.dart:211:7)
#4      ServicesBinding.initInstances (package:flutter/src/services/binding.dart:36:11)
#5      PaintingBinding.initInstances (package:flutter/src/painting/binding.dart:20:11)
#6      SemanticsBinding.initInstances (package:flutter/src/semantics/binding.dart:18:11)
#7      RendererBinding.initInstances (package:flutter/src/rendering/binding.dart:28:11)
#8      WidgetsBinding.initInstances (package:flutter/src/widgets/binding.dart:245:11)
#9      new BindingBase (package:flutter/src/foundation/binding.dart:147:5)
#10     new _Widgets<…>

我认为UI只在主线程中更新,所以我应该在分离株之间进行一些通信。不幸的是,我不知道如何处理它。我将感谢任何帮助的家伙。
在此先表示衷心的感谢!

thigvfpy

thigvfpy1#

正如您正确提到的,您只能在主线程上运行UI更新,而不能在通过compute()创建的任何其他分离菌株上运行UI更新。
所以你要做的第一件事就是

WidgetsFlutterBinding.ensureInitialized();

然后,为了与隔离进行简单的通信,您还可以返回值并在FutureBuilder中使用它们。

static Future<string> addNewChildStatic(Map map) async {
    // Perform some heavy async computation tasks
    // Return a result object in the end (in this case, a string).
    return "Finished calculation"
}

以通过

FutureBuilder(
    future: compute(_runningFunction, 10),
    builder: (context, snapshot) {
        if (snapshot.hasData) {
            return Text(snapshot.data);
        } else {
            return CircularProgressIndicator();
        }
  },
),

相关问题