Home > database >  how to get specific information from list in flutter
how to get specific information from list in flutter

Time:12-07

I'm developing a flutter application composed by three screens , the first one is a list view widget were I can fetch the data from a list variable , something like this:

here is the list view:

 child: ListView.builder(
                    padding: EdgeInsets.only(
                      top: 10.0,
                    ),
                    itemCount: data.length,
                    itemBuilder: (context, index) {
                      final 2data =data[index];

                      return GestureDetector(
                        onTap: () {
                          Navigator.push(context, 
                              MaterialPageRoute(builder: (context) {
                            return dataSecondScreen(data: 2data,);
                          }));
                        },

here is the list where I define the data

final List<Data> data = [ //Data is a class
  Data(
    1, //id
    "Eli", //name
    18, //years old

    ),

 Data(
    2, //id
    "Axel", //name
    23, //years old

)]

When I press a list view item, it redirects me to another screens were it show me all the information of the list (depending the index that I press before, I mean, If I press a item with index 1 , in the second screen I can see the información of the item with index 1)

Here is my problem, in the second screen, where I have all the information of the item with index 1 ,I have a clickable image, and I want something like this: if I click that image I want to change the state (or move to another screen) with different index , for example, If I'm in the screen with all the information of the index 1, I want to be able to click the image and change the information of the index 1 screen for the index 2 (or 3,4 ,5...). so, How I can be able to select and show the information that I want from a list?

CodePudding user response:

you should use State management packages to manage your state with better performance and easier development. here are some of the state management packages to see:

1-https://pub.dev/packages/flutter_bloc

2-https://pub.dev/packages/provider

the first one is bigger and suitable for big scale projects and the second is for a medium and small projects. Although that's fine to use each of them for your projects.

CodePudding user response:

I think you want to use the Widget PageView for this purpose: https://api.flutter.dev/flutter/widgets/PageView-class.html

You can use the PageView.builder to iterate through the list of items.

For example:

Suppose this is what your data looks like:

class Profile {
  final int id;
  final String name;
  final int age;

  Profile(this.id, this.name, this.age);
}

And a list of item:

List<Profile> data = [Profile(1, "Eli", 18), Profile(2, "Axel", 23)];

Then in the build method of your page:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: PageView.builder(
        itemBuilder: (context, index) {
          return Text(data[index].name);
        },
        itemCount: data.length,
      ),
    );
  }

If you scroll left, it will go to data[2], if you scroll right, it will go to data[1].

  • Related