Flutter 不显示 FutureBuilder 错误屏幕

Flutter Do not show FutureBuilder error screen

我使用包 flutter_native_splash 来显示启动画面。在此期间,我想实例化一个 Future,完成后,我想删除启动画面并通过 FutureBuilder 构建一个小部件。问题是设备在构建小部件之前显示启动画面和 ErrorScreen 几毫秒(我不想要它)。

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late Future<Metadata> futureMetadata;

  @override
  void initState() {
    super.initState();
    initialization();
  }

  void initialization() async {
    futureMetadata = fetchMetadata();
    FlutterNativeSplash.remove();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: FutureBuilder<Metadata>(
            future: futureMetadata,
            builder: (BuildContext context, AsyncSnapshot<Metadata> snapshot) {
              if (snapshot.hasData) {
                return NavBar(data: snapshot.data);
              } else {
                return const ErrorScreen();
              }
    }
        )
    );
  }
}

FlutterNativeSplash.remove(); 删除启动画面。

因为你想在加载完成后删除启动画面,我想你想要的不是

futureMetadata = fetchMetadata();
FlutterNativeSplash.remove();

futureMetadata = fetchMetadata();
await futureMetadata;
FlutterNativeSplash.remove();