Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : Prevent FutureBuilder always refresh every change screen

Tags:

flutter

dart

I have FutureBuilder to fetch User profil from API and code to fetch user like this :

 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

If you can see in above GIF, My FutureBuilder are inside BottomNavigationBar. Every i change the screen/page from BottomNavigationBar and come back to my FutureBuilder is always refresh !

How can i fixed it to only once to refresh ?

Home Screen

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(),
                );
              }
            },
          ),
        ],
      ),
    );
  }
}

Shared Preferences Function

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();

Update Question

I already try Initialize FutureBuilder and use initState and didChangeDependencies . But new problem is , if i initialize inside initState my profil not rebuild because Provider listen=false. If i using didChangeDependencies my FutureBuilder still refresh every i change screen. Something wrong ?

Using initState

enter image description here

Using didChangeDependencies

enter image description here

like image 552
Zeffry Reynando Avatar asked Feb 10 '20 03:02

Zeffry Reynando


1 Answers

Initialize the Future during initState or didChangeDependencies instead.

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) {
              // ...
            },
          ),
        ],
      ),
    );
  }
}
like image 58
mazei513 Avatar answered Sep 18 '22 00:09

mazei513