class_SampleAppPageStateextendsState<SampleAppPage> { List<Map<String, Object?>> data = [];
@override void initState() { super.initState();
/// 主1. 加载数据 loadData(); }
boolget showLoadingDialog => data.isEmpty;
Future<void> loadData() async { /// Opens a long-lived port for receiving messages. /// 打开端口用于接收数据 final ReceivePort receivePort = ReceivePort();
/// 主2.Isolate开启子线程 /// The [entryPoint] function must be able to be called with a single /// argument, that is, a function which accepts at least one positional /// parameter and has at most one required positional parameter. /// /// The entry-point function is invoked in the new isolate with [message] /// as the only argument. /// 第一个参数:至少包含一个参数的函数指针,这里关联的是dataLoader,参数是SendPort /// /// [message] must be sendable between isolates. Objects that cannot be sent /// include open files and sockets (see [SendPort.send] for details). Usually /// the initial [message] contains a [SendPort] so that the spawner and /// spawnee can communicate with each other. /// 第二个参数: 不同Isolate之间传递的数据,通常初始化时传的message包含一个SendPort /// /// receivePort.sendPort /// [SendPort]s are created from [ReceivePort]s. /// Any message sent through a [SendPort] is delivered to its corresponding [ReceivePort]. /// There might be many [SendPort]s for the same [ReceivePort]. /// 通过SendPort发送的消息会传送给关联的ReceivePort await Isolate.spawn(dataLoader, receivePort.sendPort);
/// 下面这种写法在sendReceive会报 /// Unhandled /// Exception: type 'Future<dynamic>' is not a subtype of type /// 'Future<List<Map<String, dynamic>>>' /// /// replyTo.send(jsonDecode(response.body) as List<Map<String, dynamic>>); /// 因为Dart在运行时无法检查Future<T>中的T,直接转换Future的泛型参数会失败 /// 强制类型转换 final data = jsonDecode(response.body) asList; final typedata = data.cast<Map<String, dynamic>>();