Sunday 24 October 2021

Nested Stream Builder not updating when trying to setstate

So I am trying to get the data from assets folder in first streambuilder and in the second I am getting data from sharedpreference in the second shared preference were the list of theme is selected based on the sharedpreference position but when i try to update the data and comes to the screen from theme screen the data is getting changed and returning properly in the provider class but the snapshot.data is returning the old value for the nested streambuilder. both the streambuilder is getting called only the snapshot data is returning old data.

 StreamBuilder(
                  stream: _stream,
                  builder: (context, AsyncSnapshot snapshot) {
                    if (snapshot.hasData) {
                      List<String> lines = snapshot.data;
                      print('${quotesdata.themebg}');
                      quotesdata.getthemedat(context);
                      return Screenshot(
                        child: Stack(
                          children: [
                            StreamBuilder(
                                stream: data,
                                builder: (context, AsyncSnapshot snapshot) {
                                  if (snapshot.hasData) {
                                    BackgroundTheme databg =
                                        snapshot.data as BackgroundTheme;
                                    print('${quotesdata.themebg}');
                                    print(databg.backgroundImageUrl);
                                    return Container(
                                      height: size.height,
                                      decoration: BoxDecoration(
                                          image: DecorationImage(
                                              image: NetworkImage(
                                                databg.backgroundImageUrl,
                                              ),
                                              fit: BoxFit.cover)),
                                    );
                                  } else {
                                    return Container(
                                      height: size.height,
                                    );
                                  }
                                }),
                            ScreenShotWidget(
                              homeViewModel: quotesdata,
                              list: lines,
                            ),
                          ],
                        ),
                        controller: screenshotController,
                      );
                    } else {
                      return Container();
                    }
                  }),

and provider methods

Future<BackgroundTheme?> getthemedat(BuildContext context) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int? theme = prefs.getInt('theme');
ThemeDataModel themelist = await locator<JsonAPI>().fetchtheme(context);
if (theme != null) {
  themebg = themelist.backgroundTheme[theme];
} else {
  themebg = themelist.backgroundTheme[0];
}
print('${themebg!.backgroundImageUrl}');
notifyListeners();
return themebg;
}

 Future<List<String>> fetchdata(BuildContext context) async {
lines.clear();

SharedPreferences prefs = await SharedPreferences.getInstance();
String? category = prefs.getString('category');
if (category != null) {
  lines = await locator<JsonAPI>().fetchquotes(category);
} else {
  lines = await locator<JsonAPI>().fetchquotes('quotes');
}
// data = lines as Future<List<String>>;
//  getthemedat(context);
notifyListeners();
return lines;
}

Any help or other approach will be welcome .



from Nested Stream Builder not updating when trying to setstate

No comments:

Post a Comment