Dynamic News Loading with Scrollable ListView in Flutter - flutter

Dio dio = Dio();
List<News> news = [];
Future<void> fetchNewss() async {
Response response =
await dio.get("http://52.90.175.175/api/news/get?page=$currentPage");
var data = response.data["data"]["data"] as List;
setState(() {
news.addAll(data.map((i) => News.fromJson(i)).toList());
});
}
this is how i fetch my news from my server and per page 10 news are coming and but I need to other page news to be added with in existing page 1 news list like when I scroll to bottom of listview builder it takes like 1 sec and load page 2 news and show 1st page and 2nd page news and it should be scrollable I tried a package but it does not scroll the full screen here is my full code

Related

Flutter dispose provider notifyListners()

I have a simple controller like this
class MatchListController with ChangeNotifier {
List<MatchData> liveMatches = [];
MatchListController() {
getMatchList();
}
getMatchList() async {
debugPrint('match list api called');
var response = await ApiService().matchList();
if (response != null) {
final databody = json.decode(response);
notifyListeners();
}
}
}
When my page loads it runs the function getMatchList() which is fine but If I change the page between function and when it return data notifyListeners hit and app crash. I am using loading but I have tab menu so I can change the page from there What I want is if page is close then notifyListeners dispose.
The answer that khalil mentioned is correct, there is also another solution:
Since you are using loading, I assume you want to prevent the page from changing, so you can wrap the tab menu buttons with an AbsorbPointer widget, and use setState to change its absorbing state according to your loading state. Your loading state can simply be a boolean inside your stateful widget.

Fetch & show item details

I'm currently learning Flutter and developing a Shopping List.
In the backend, there's a Laravel instance providing the following Endpoints:
/lists -> A list of all shopping lists
/lists/{listId}/items -> All items in the list
For the Flutter side, I've created a provider which fetches all lists from the server.
Future<void> init() async {
this.lists = await this.service.getLists();
notifyListeners();
}
This data (<List<ListModel>>) is retreived by my widget and displayed through ListView.builder. The RefreshIndicator just refreshes the provider data by run the init method again.
Widget build(BuildContext context) {
final provider = Provider.of<ShoppingListProvider>(context);
List<ListModel> lists = provider.lists;
...
child: RefreshIndicator(
onRefresh: () => provider.init(),
Now, if I click on the ListTile, I'd like to display all items. They should be fetched from the API as well, and I'd like to refresh the item list too. Unfortunately, I don't manage to get this data provided through the provider, as it requires the "listId".
Maybe someone can give me a hint on this?
Fetched from inside the widget, which works, but then I can't refresh the data. It somehow should be provided through the provider

I am trying to get user Facebook profile picture inside a drawer in flutter but it get into infinite loop here is the code . Flutter

final facebookUserData =FacebookAuth.instance.getUserData().then((facebookUserData) async {
String profilePicture = facebookUserData['picture']['data']['url'];
textProvider.facebookProfilePhoto(profilePicture);
});
I am using Provider to change the value to a new one that comes from the User data.

Flutter: conditional bottom navigation bar, to show pages based on if condition

I have implemented a bottom navigation bar with the logic listed below, and I want to check if the user is logged in. If user is logged in on third tab I want to show the profile page instead of SignIn page. This is my code where I am displaying the bottom navigation bar, I am stuck on what to do after checkIsLoggedIn() async function.
After that Im building list of widgets to show the pages in 3 tabs.
code of which is below,
so in place of SignIn(), I need to show, if the user is logged in it shows signin other wise, it shows Profile page.
Please do help me out here.
This may help.
Future<void> checkIfLoggedIn() async {
SharedPreferences localStorage = await SharedPreferences.getInstance();
var id = localStorage.getString('id');
if (id != null) {
setState(() {
_children[2] = ProfilePage();
});
}
}
Inside checkIfLoggedIn(), along with _isLoggedIn write the following code: _children[2] = Profile().
You can set the last BottomNavigationItem as below
BottomNavigationItem(
icon:_isLoggedIn ? Icons.person : Icons.key,
title:Text(_isLoggedIn ? 'Account' : 'Log in' )
)
and you can handle the click event and set the proper page in the build() by the same ternary operators using the _isLoggedIn variable
build(){
return Scaffold(
body:_isLoggedIn?ProfilePage():LoginPage()
);
}

How do I handle returning to the previous page in Flutter?

How do I handle returning to the previous page in Flutter?
If I did Navigator.push, how to perform some actions when second page gets closed?
You "await" for the page to "pop"
Future<void> pushScreenAndDoSomething(BuildContext context)async{
await Navigator.of(context).pushNamed('YourPage');
print('This executes after the screen is destroyed');
//you can also get a result
String p = await Navigator.of(context).pushNamed('YourPage');
//Inside your page, you Navigator.of(context).pop('DATA');
print(p); //it should print DATA
}