Home > Blockchain >  Issues with the lifecycle of a widget
Issues with the lifecycle of a widget

Time:09-17

I have a MaterialPageRoute list that I pass a GridView.count () to mount for each of the routes. The failure occurs when I move from one of those MaterialPageRoute and go back and repeat in the same, it throws me an exception:

**════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4523 pos 12: 'entry.route._navigator == null': is not true.**

This is my GridView:

    GridView menuGrid(BuildContext context, List<MaterialPageRoute> lista,
        int columnas, int border) {
      return GridView.count(
        crossAxisCount: columnas,
        padding: const EdgeInsets.all(20),
        crossAxisSpacing: 10,
        mainAxisSpacing: 10,
        children: <Widget>[
          ..._listado(context, lista, border),
        ],
      );
    }

This is the list:

List<Widget> _listado(
    BuildContext context, List<MaterialPageRoute> lista) {
  List<Widget> _listaWidget = [];
  lista.forEach((element) {
    _listaWidget.add(GestureDetector(
      onTap: () {
        Navigator.push(context, element);
      },
      child: Container(
          padding: const EdgeInsets.all(5),
          child: Container(
            padding: const EdgeInsets.all(8),
            decoration: BoxDecoration(
              image: DecorationImage(
                image: Image.asset('assets/images/image1.png').image,
                fit: BoxFit.cover,
              ),
              border: Border.all(
                color: Colors.white,
                width: 2,
              ),
              borderRadius: new BorderRadius.only(
                topRight: const Radius.circular(20.0),
                bottomRight: const Radius.circular(20.0),
                topLeft: const Radius.circular(20.0),
                bottomLeft: const Radius.circular(20.0),
              ),
              boxShadow: [
                BoxShadow(
                  color: Colors.white.withOpacity(0.5),
                  spreadRadius: 1,
                  blurRadius: 3,
                  offset: Offset(
                    4,
                    4,
                  ), // changes position of shadow
                ),
              ],
            ),
          )),
    ));
  });
  return _listaWidget;
}

The list for example:

List<MaterialPageRoute> lista= [
  MaterialPageRoute(builder: (_) => Page1()),
  MaterialPageRoute(builder: (_) => Page2()),
];

CodePudding user response:

I found the solution. I created a abstract class to call my navegation objects:

abstract class ElementoNavegacion {
  Image getImagen();
  MaterialPageRoute show();
}

class ElementoDatos extends ElementoNavegacion {
  Image getImagen() {
    return getImageWidget("datos");
  }

  MaterialPageRoute show() {
    return MaterialPageRoute(builder: (_) => DatosPage());
  }
}

And i call -> Navigator.push(context, element[0].show()) This works because the instance is not lost in memory.

  • Related