Home > Net >  Flutter Do not show FutureBuilder error screen
Flutter Do not show FutureBuilder error screen

Time:04-12

I use the package flutter_native_splash to display a splash screen. During this time, I want to instantiate a Future and, when it's done, I want to remove the splash screen and build a widget thanks to a FutureBuilder. The problem is that the device displays the splash screen AND the ErrorScreen for few milliseconds (I don't want it) before building the widget.

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(); removes the splash screen.

CodePudding user response:

because you want to remove the splashscreen after it's finished loading I think you want instead of

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

do

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