扑扑闪屏与尝试抓住

时间:2019-08-22 01:53:40

标签: flutter

  Widget build(BuildContext context) {
  try {
      //---------------------------------------------->internet assscess confirm
      InternetAddress.lookup('google.com').then((vaule) async {
        result = vaule;
        if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
          if (await checkDB() == false) {
            path = await createDB(); ------------->db check
          }
          path = await getDatabasesPath();
          try {
            await Bloc.setLocationCode(); ---------->location check
          } catch (_) {
            //---------------------------//
          }
          await getValue('levelKey').then((value) {
            if (value != null) {
              ChildDrawerState.levelSwitch = value;
            } else {
              ChildDrawerState.levelSwitch = false;
            }
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) =>RouteMain(path)));
          });
        }
      });
    } on SocketException catch (_) {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => ErrorMain()));
    }
    return Scaffold(
      body: Container(
        child: Image.asset(
          'image/splash-screen.gif',
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
        ),
      ),
    );
  }
}

我像上面一样制作了启动画面类和构建方法。 但是问题是构建方法被撤回(重新构建),所以子部件也被重新构建了很多次,我该如何解决?

2 个答案:

答案 0 :(得分:1)

您需要改用StatefulWidget,然后将try-catch语句移至initState()。

答案 1 :(得分:1)

操作方法

  • 将屏幕创建为有状态
  • 将访问互联网的方法移动到新的 async 功能
  • 中 通过重写initState
  • 调用我们新的 async函数

完整代码

import 'package:flutter/material.dart';

class MainScreen extends StatefulWidget {
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  void accessInternet() async {
    try {
      //---------------------------------------------->internet assscess confirm
      InternetAddress.lookup('google.com').then((vaule) async {
        result = vaule;
        if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
          if (await checkDB() == false) {
            path = await createDB(); // ------------->db check
          }
          path = await getDatabasesPath();
          try {
            await Bloc.setLocationCode(); // ---------->location check
          } catch (_) {
            //---------------------------//
          }
          await getValue('levelKey').then((value) {
            if (value != null) {
              ChildDrawerState.levelSwitch = value;
            } else {
              ChildDrawerState.levelSwitch = false;
            }
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => RouteMain(path)));
          });
        }
      });
    } on SocketException catch (_) {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => ErrorMain()));
    }
  }

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

  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Image.asset(
          'image/splash-screen.gif',
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
        ),
      ),
    );
  }
}