// config dart
import 'package:flutter/material.dart';
import 'package:offline_solution/pages/pages.dart';
Map<int, Map<String, dynamic>> bottomBar = {
0: {"path": "Home", "icon": Icons.home, "page": HomeView, "initial": true},
1: {
"path": "CM",
"icon": Icons.dashboard,
"page": CMView,
"initial": false
},
2: {
"path": "PM",
"icon": Icons.view_agenda,
"page": PMView,
"initial": false
},
3: {
"path": "Messages",
"icon": Icons.notifications,
"page": NotificationView,
"initial": false
}
};
Declaro un Mapa para la configuración de la ruta, y HomeView, CMView, etc., son todos StatelessWidget.
El mapa de arriba se utilizará aquí:
import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:offline_solution/config/bottom_bar.dart';
import 'package:offline_solution/routes/bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class RoutesSwitcher extends StatelessWidget {
RoutesSwitcher({Key? key}): super(key: key);
@override
Widget build(BuildContext context) {
var routeIndex = context.watch<RouteBloc>().state.routeIndex;
Map<String, dynamic> routeConfig = bottomBar[routeIndex]!;
return PageTransitionSwitcher(
duration: const Duration(milliseconds: 300),
transitionBuilder: (
Widget child,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return SharedAxisTransition(
child: child,
animation: animation,
secondaryAnimation: secondaryAnimation,
transitionType: SharedAxisTransitionType.horizontal,
);
},
child: routeConfig['page'](),
);
}
Entonces ocurre un error:
ingrese la descripción de la imagen aquí
Una excepción ha ocurrido.
NoSuchMethodError (NoSuchMethodError: se intentó usar el tipo 'HomeView' como una función. Dado que los tipos no definen una 'llamada' al método, esto no es posible. ¿Tenía la intención de llamar al constructor de HomeView y olvidar el operador 'nuevo'?
Receptor: HomeView
Intenté llamar: HomeView()
Solución del problema
en tu mapa
Map<int, Map<String, dynamic>> bottomBar = {
0: {"path": "Home", "icon": Icons.home, "page": HomeView, "initial": true},
1: {
"path": "CM",
"icon": Icons.dashboard,
"page": CMView,
"initial": false
},
2: {
"path": "PM",
"icon": Icons.view_agenda,
"page": PMView,
"initial": false
},
3: {
"path": "Messages",
"icon": Icons.notifications,
"page": NotificationView,
"initial": false
}
};
estás pasando HomeView
, CMView
y otros como una función, no como un Widget. Prueba a cambiarlo por HomeView()
. Cuando tiene "()", está llamando a un objeto o un widget. Cuando no lo tiene, es una función o un tipo de clase. Entonces el código está leyendo esto como una función. ¡Dame un comentario si funciona!
No hay comentarios:
Publicar un comentario