-1

I want to convert this list string to list stateful widget

List<String> listString = [
"HomePage()",
"ProfilePage()"
]

to

List<StatefulWidget> listStatefulWidget = [];

expected result :

List<StatefulWidget> listStatefulWidget = [
HomePage(),
ProfilePage()
];

update : I need this for my navbar that require a list of StatefulWidget. tabs is list of StatefulWidget thats should be get the value from API. my API return listString, so I need to convert it to statefulWidget.

here my code:

var tabs<StatefulWidget> = [
    HomePageView() 
    AccountView(),
  ];

return Scaffold(
    key: scaffoldKey,
    backgroundColor: Theme.of(context).scaffoldBackgroundColor,
    bottomNavigationBar: CurvedNavigationBar(
      key: _bottomNavigationKey,
      index: 0,
      height: 65.0,
      items: [
       //navbar 
      ],
      color: Colors.white,
      buttonBackgroundColor: kSecondaryColor,
      backgroundColor: Colors.transparent,
      animationCurve: Curves.easeInOutQuint,
      animationDuration: Duration(milliseconds: 300),
      onTap: (index) {
        setState(() {
          _page = index;
        });
      },
      letIndexChange: (index) => true,
    ),
    body: tabs[_page]);

}

Soveyyy
  • 274
  • 4
  • 17
  • what are you trying to achieve ? – Nishuthan S Jun 28 '21 at 04:32
  • @NishuthanS shortly I got list of string from API (ex: "HomePage()"). then I want to convert it to StatefulWidget that uses for Navigate to each class in Bottom Navigation Bar – Soveyyy Jun 28 '21 at 04:39

1 Answers1

1

You cannot convert a String to a StatefulWidget.

What you can do is use the string with NavigatorState to navigate to parts of the app.

Global key for NavigatorState

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

main.dart

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      navigatorKey: navigatorKey,
      onGenerateRoute: (routeSettings) {
      switch (routeSettings.name) { //add more routes as needed
        case 'Home':
          return MaterialPageRoute(builder: (context) => Home());
        default:
          return MaterialPageRoute(builder: (context) => Profile());
      }
    },
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

Navigating to the page

You can use this navigatorKey.currentState!.pushNamed(<Route Name>) to navigate to the page.

Example : navigatorKey.currentState!.pushNamed('Home') This will navigate you to Home page.

In your case, you can get the routes from an API and build a bottom Navigation bar and use the string to navigate to pages.

Example:

navigatorKey.currentState!.pushNamed(listStatefulWidget [index]);

Refer:

https://stackoverflow.com/a/53397266/9074190

https://www.filledstacks.com/post/navigate-without-build-context-in-flutter-using-a-navigation-service/

Nishuthan S
  • 1,538
  • 3
  • 12
  • 30
  • Thanks for the answer Mr. @Nisuthan S, appreciate it. What I wanted is listofStatelesWidget from String coz navigation bar required list of statefulwidget. I have updated my question. If you dont mind, can you check it please? Thank you. – Soveyyy Jun 30 '21 at 08:58