【发布时间】:2023-01-02 05:36:05
【问题描述】:
我正在尝试使用 FutureBuilder 加载一个大文件并使用以下代码进行隔离。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
// utils
const fileName = 'assets/files/big_file.txt';
const smallFileName = 'assets/files/small_file.txt';
//
Future<String> loadBigFile(String fileName) async {
return rootBundle.loadString(fileName);
}
Future<String> loadFileUsingThread(String fileName) async {
return await compute(loadBigFile, fileName);
}
稍后当我尝试调用下面的内容时
Future<String>? _future;
_future = loadFileUsingThread(smallFileName);
child: FutureBuilder<String>(
initialData: '',
future: loadFileUsingThread(smallFileName),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
return SingleChildScrollView(
child: Text(snapshot.data.toString()),
);
}
return Center(
child: Text(
'File Read Error ${snapshot.error}',
));
},
),
)
我收到以下错误
Unhandled Exception: Binding has not yet been initialized.
The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding.
In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the "instance" getter.
【问题讨论】:
-
请edit你的问题分享给你
main()入口函数 -
我无法重现。也许尝试重建应用程序...
-
我做了一切。坚持了2天。
标签: flutter mobile dart-isolates