Flutter:阻止FutureBuilder始终刷新每个更改屏幕

时间:2020-02-10 03:14:21

标签: flutter dart

我有FutureBuilder可以从API获取用户配置文件,并可以通过代码获取用户:

 Future<List<UserModel>> getUserByUsername({@required String username}) async {
    try {
      final response =
          await _client.get("$_baseUrl/getUserByUsername?username=$username");
      final Map<String, dynamic> responseJson = json.decode(response.body);
      if (responseJson["status"] == "ok") {
        List userList = responseJson['data'];
        final result = userList
            .map<UserModel>((json) => UserModel.fromJson(json))
            .toList();
        return result;
      } else {
        throw CustomError(responseJson['message']);
      }
    } catch (e) {
      return Future.error(e.toString());
    }
  }

future builder

如果您可以在上面的GIF中看到,则我的FutureBuilder位于BottomNavigationBar中。我每次从BottomNavigationBar更改屏幕/页面并回到我的FutureBuilder时总是刷新!

我如何将其固定为仅刷新一次?

主屏幕

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    final username = Provider.of<SharedPreferencesFunction>(context).username;
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          CardTime(),
          FutureBuilder(
            future: userApi.getUserByUsername(username: username),
            builder: (BuildContext context,
                AsyncSnapshot<List<UserModel>> snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                if (snapshot.hasError) {
                  return Center(
                    child: Text(
                      snapshot.error.toString(),
                    ),
                  );
                } else {
                  final user = snapshot.data[0];
                  return CardProfil(
                    imageUrl: "${userApi.baseImageUrl}/${user.fotoUser}",
                    detailProfil: [
                      Text(
                        user.namaUser,
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      Text(user.idDevice),
                    ],
                  );
                }
              } else {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            },
          ),
        ],
      ),
    );
  }
}

共享首选项功能

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

class SharedPreferencesFunction extends ChangeNotifier {
  SharedPreferencesFunction() {
    initialSharedPreferences();
    getUsername();
  }
  String _username;
  String get username => _username;

  void initialSharedPreferences() {
    getUsername();
  }

  Future updateUsername(String username) async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    await pref.setString("username", username);
    //! It's Important !!! After update / remove sharedpreferences  , must called getUsername() to updated the value.
    getUsername();
    notifyListeners();
  }

  Future removeUsername() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    final result = await pref.remove("username");
    //! It's Important !!! After update / remove sharedpreferences  , must called getUsername() to updated the value.
    getUsername();
    print(result);
    notifyListeners();
  }

  Future getUsername() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    final result = pref.getString("username");
    _username = result;
    notifyListeners();
  }
}

final sharedpref = SharedPreferencesFunction();

更新问题

我已经尝试初始化FutureBuilder并使用 initState didChangeDependencies 。但是新的问题是,如果我在 initState 内部初始化,则由于 Provider listen = false ,我的配置文件无法重建。 如果我使用 didChangeDependencies ,我的FutureBuilder仍会刷新每个我更改的屏幕。 有什么问题吗?

使用initState

enter image description here

使用didChangeDependencies

enter image description here

3 个答案:

答案 0 :(得分:4)

FutureinitState期间初始化didChangeDependencies

class _HomeScreenState extends State<HomeScreen> {
  Future<List<UserModel>> user;

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

    // must use listen false here
    final username = Provider.of<SharedPreferencesFunction>(context, listen: false).username;
    user = userApi.getUserByUsername(username: username);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    final username = Provider.of<SharedPreferencesFunction>(context).username;
    user = userApi.getUserByUsername(username: username);
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          FutureBuilder(
            future: user,
            builder: (context, snapshot) {
              // ...
            },
          ),
        ],
      ),
    );
  }
}

答案 1 :(得分:1)

我遇到了类似的情况,并在每个视图/页面/标签栏视图/小部件/子项上使用 AutomaticKeepAliveClientMixin 以保持页面在我每次来回浏览标签栏时都不会刷新。

class YourClass extends StatefulWidget {
  YourClass({
    Key key
  }): super(key key);

  @override
  _YourClassState createState() => _YourClassState();
}

// Must include AutomaticKeepAliveClientMixin
class _YourClassState extends State<YourClass> with AutomaticKeepAliveClientMixin {
  Future resultGetData;

  void getData() {
    setState(() {
      resultGetData = getDataFromAPI();
    });
  }

  // Must include
  @override
  bool get wantKeepAlive => true;

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

  @override
  Widget build(BuildContext context) {
    super.build(context); // Must include
    return FutureBuilder(
      future: resultGetAllByUserIdMCId,
      builder: (context, snapshot) {
        // ...
        // Some Code
        // ...
      }
    );
  }

}

如果您想刷新数据,您可以使用运行 RefreshIndicator 函数的 getData()。将此代码放在 FutureBuilder 中。 key: PageStorageKey(widget.key) 会将卷轴保持在您离开的位置。

return RefreshIndicator(
  onRefresh: () async {
    getData();
  },
  child: ListView.separated(
    key: PageStorageKey(widget.key),
    itemCount: data.length,
    separatorBuilder: (BuildContext context, int index) {
      return Divider(height: 0);
    },
    itemBuilder: (context, index) {
      return ...;
    },
  ),
);

答案 2 :(得分:0)

使用IndexedStack作为选项卡的父级。