Creating Listeners in Gridview using a List Flutter

Asked

Viewed 70 times

0

I’m creating a menu with a gridview on the flutter. Shares are on a List:

List<Map> actions = [
{"description": "Cadastro", "icon": Icons.people, },
{"description": "Alterar Senha", "icon": Icons.security},
{"description": "Contra Cheque", "icon": Icons.attach_money},
{"description": "Histórico de Contribuições", "icon": Icons.print},
{"description": "Sair", "icon": Icons.directions_run, "onTap": logout},]

I intend to put the actions there too, as in the case of the action go calling the logout method on onTap. the method:

static void logout(BuildContext context) {
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => LoginScreen(),));}

So, finally, in the gridview Builder I call a method to generate each action:

 Widget createGridViewTile(int index) {
return GestureDetector(
  onTap: actions[index]["onTap"] != null ? actions[index]["onTap"](context) : null,
  child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      IconButton(
        iconSize: 70,
        onPressed: () {

        },
        icon: Icon(
          actions[index]["icon"],
          color: widget.primaryColor,
        ),

      ),
      Text(
        actions[index]["description"],
        textAlign: TextAlign.center,
        style: TextStyle(
          color: widget.primaryColor,
          fontSize: 14,
        ),
      )
    ],
  ),
);}

However, in the line that creates onTap is returning the following error (if taking the line from the ontap works):

setState() or markNeedsBuild() called during build.

E/flutter (19906): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: 'package:flutter/src/widgets/Navigator.Dart': Failed assertion: line 2334 pos 18: '!Navigator. _debugLocked': is not true. E/flutter (19906): #0 _Assertionerror. _doThrowNew (Dart:core-patch/errors_patch.Dart:42:39) E/flutter (19906): #1 _Assertionerror. _throwNew (Dart:core-patch/errors_patch.Dart:38:5) E/flutter (19906): #2 _Routeentry.handlePush. (package:flutter/src/widgets/Navigator.Dart:2334:18) E/flutter (19906): #3 Tickerfuture.whenCompleteOrCancel.thunk (package:flutter/src/Scheduler/Ticker.Dart:398:15) E/flutter (19906): #4 _rootRunUnary (Dart:async/zone.Dart:1192:38) E/flutter (19906): #5 _Customzone.runUnary (Dart:async/zone.Dart:1085:19) E/flutter (19906): #6 _Futurelistener.handleValue (Dart:async/future_impl.Dart:141:18) E/flutter (19906): #7 Future. _propagateToListeners.handleValueCallback (Dart:async/future_impl.Dart:682:45) E/flutter (19906): #8 Future. _propagateToListeners (Dart:async/future_impl.Dart:711:32) E/flutter (19906): #9 Future. _completeWithValue (Dart:async/future_impl.Dart:526:5) E/flutter (19906): #10 Future. _asyncComplete. (Dart:async/future_impl.Dart:556:7) E/flutter (19906): #11 _rootRun (Dart:async/zone.Dart:1184:13) E/flutter (19906): #12 _Customzone.run (Dart:async/zone.Dart:1077:19) E/flutter (19906): #13 _Customzone.runGuarded (Dart:async/zone.Dart:979:7) E/flutter (19906): #14 _Customzone.bindCallbackGuarded. (Dart:async/zone.Dart:1019:23) E/flutter (19906): #15 _microtaskLoop (Dart:async/schedule_microtask.Dart:43:21) E/flutter (19906): #16 _startMicrotaskLoop (Dart:async/schedule_microtask.Dart:52:5) E/flutter (19906):

Someone can give me a light?

1 answer

2


Along those lines:

onTap: actions[index]["onTap"] != null ? actions[index]["onTap"](context) : null,

There is the element actions[index]["onTap"](context).

How did you put (context) this is not passing a function as a parameter, but the return of it.

To correct, you could try to place the function directly, if not for the need of the parameter context that its function requires and the Gesturedetector does not pass in the onTap:

onTap: actions[index]["onTap"] != null ? actions[index]["onTap"] : null,
//Precisa do context

To solve this, one of the alternatives is to create an anonymous function that calls yours, for example as follows:

onTap: actions[index]["onTap"] != null ? () => actions[index]["onTap"](context) : null,

A deeper insight into the error can be read here (in English).

  • Understood the error! It worked perfectly now, thank you!

  • You are welcome! Do not forget to mark the answer as accepted, if found valid, so the question is resolved and help any site searches.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.