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,
),
),
);
}
}
我像上面一样制作了启动画面类和构建方法。 但是问题是构建方法被撤回(重新构建),所以子部件也被重新构建了很多次,我该如何解决?
答案 0 :(得分:1)
您需要改用StatefulWidget,然后将try-catch语句移至initState()。
答案 1 :(得分:1)
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,
),
),
);
}
}