0

My first screen has a sheet "static List<DilemmData> dilemmList = [];" which should be displayed in the ListView. I update this sheet on the second screen. But when I go back to the first one, there is no change. How to fix it?

Here is ListView:

ListView.separated(
              separatorBuilder: (ctx, index) =>
                  const SizedBox(height: 15.0),
              itemCount: MyApp.dilemmList.length,
              itemBuilder: (ctx, index) {
                return Padding(
                  padding: const EdgeInsets.only(left: 25, right: 25.0),
                  child: Container(
                    decoration: BoxDecoration(
                      color: const Color(0xFF009432),
                      border:
                          Border.all(width: 2.0, color: Colors.transparent),
                      borderRadius: BorderRadius.circular(10.0),
                    ),
                    child: ListTile(
                      onTap: () {},
                      minLeadingWidth: 32,
                      leading: CircularPercentIndicator(
                        radius: 25.0,
                        lineWidth: 4.0,
                        percent: MyApp.dilemmList[index].percent,
                        center: Text(
                            '${(MyApp.dilemmList[index].percent * 100).round()}%',
                            style: GoogleFonts.roboto(
                                fontSize: 12,
                                color: Colors.white,
                                fontWeight: FontWeight.w700)),
                        progressColor: const Color(0xFF3ae374),
                      ),
                      title: Text(MyApp.dilemmList[index].title,
                          style: GoogleFonts.roboto(
                              fontSize: 20,
                              color: Colors.white,
                              fontWeight: FontWeight.w700)),
                      subtitle: Text(MyApp.dilemmList[index].date,
                          style: GoogleFonts.roboto(
                              fontSize: 15,
                              color: Colors.white,
                              fontWeight: FontWeight.w700)),
                    ),
                  ),
                );
              }),

Here I am adding a new element to the sheet:

MyApp.dilemmList.add(DilemmData(
    percent: 0,
    title: controller.text,
    date: formattedDate));
Alexandr
  • 63
  • 5

1 Answers1

0

Yeasin Sheikh is right but I would recommend using a provider, possibly a consumer builder: Provider

This will help you update the previous page but you will have to declare the provider at a global position: a place accessible to both pages.

void main(){
  runApp(MyApp());
}

class MyApp extends StatefulWidget {

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

class _MyAppState extends State<MyApp> {
 
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (ctx) => YourProviderClass()),
  ]),
child: HomePage(); //whatever you want.
}

You could try the Get package. Even more superior.

Georgina
  • 764
  • 2
  • 13