'Did you intend to call the constructor and forget the 'new' operator when rendering a widget

// 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
  }
};

I declare a Map for the route config, and HomeView,CMView etc., are all StatelessWidget.

The Map above will be used here:

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'](),
  );
}

Then an error occurs:

enter image description here

Exception has occurred.

NoSuchMethodError (NoSuchMethodError: Attempted to use type 'HomeView' as a function. Since types do not define a method 'call', this is not possible. Did you intend to call the HomeView constructor and forget the 'new' operator?

Receiver: HomeView
Tried calling: HomeView()



Solution 1:[1]

In your map

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
  }
};

you are passing HomeView, CMView and others as a function, not as a Widget. Try changing it to HomeView(). When you have "()", you are calling a object, or a Widget. When you don't have it, it's a function or a class type. So the code is reading this as a function. Give me a feedback if it works!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ricarDEV