gpt4 book ai didi

flutter - 在 main.dart 中使用 FutureBuilder

转载 作者:行者123 更新时间:2023-12-01 21:43:13 25 4
gpt4 key购买 nike

下面的代码总是显示 OnboardingScreen 一段时间(可能是几毫秒),然后显示 MyHomePage。我相信你们都明白我想做什么。我正在使用 FutureBuilder 检查 getString 方法是否有数据。什么是我的错?或者任何其他最好的方法?

saveString() async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('firstOpen', '1');
}

getString() 方法总是返回字符串。

getString() async {
final prefs = await SharedPreferences.getInstance();
String txt = prefs.getString('firstOpen');
return txt;
}

主飞镖

home: new FutureBuilder(
future: getString(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return MyHomePage();
} else {
return OnboardingScreen();
}
})

最佳答案

通常我使用的是另一条路线,而不是 FutureBuilder。因为 futurebuilder 每次热重载都会重置 futureBuilder。

数据加载前总会有一些延迟,因此您需要在数据加载前显示一些内容。

Snapshot.hasData 仅显示已解析 future 的返回数据。

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: SplashScreen(),
);
}
}

class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}

const isOnboardingFinished = 'isOnboardingFinished';

class _SplashScreenState extends State<SplashScreen> {
Timer timer;
bool isLoading = true;

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

Future<void> _checkIfFirstOpen() async {
final prefs = await SharedPreferences.getInstance();
var hasOpened = prefs.getBool(isOnboardingFinished) ?? false;

if (hasOpened) {
_changePage();
} else {
setState(() {
isLoading = false;
});
}
}

_changePage() {
Navigator.of(context).pushReplacement(
// this is route builder without any animation
PageRouteBuilder(
pageBuilder: (context, animation1, animation2) => HomePage(),
),
);
}

@override
Widget build(BuildContext context) {
return isLoading ? Container() : OnBoarding();
}
}

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(child: Text('homePage'));
}
}

class OnBoarding extends StatelessWidget {
Future<void> handleClose(BuildContext context) async {
final prefs = await SharedPreferences.getInstance();
prefs.setBool(isOnboardingFinished, true);
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (_) => HomePage(),
),
);
}

@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: RaisedButton(
onPressed: () => handleClose(context),
child: Text('finish on bording and never show again'),
),
),
);
}
}

关于flutter - 在 main.dart 中使用 FutureBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60971491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com