skip to Main Content

I facing this error

Error: The argument type 'Object?' can't be assigned to the parameter type 'Widget?'.
tabs_screen.dart:34
  • Object is from dart:core.
  • Widget is from package:flutter/src/widgets/framework.dart
('/C:/flutter_windows_3.3.4-stable/flutter/packages/flutter/lib/src/widgets/framework.dart').
framework.dart:1

body: _pages[_selectPageIndex]['page'].

Code

class TapScreen extends StatefulWidget {
  const TapScreen({super.key});

  @override
  State<TapScreen> createState() => _TapScreenState();
}

List<Map<String, Object>> _pages = [
  {'page': CategoriesScreen(), 'title': 'Catergories'},
  {'page': FavoitesScreen(), 'title': 'your Favorites'}
];

class _TapScreenState extends State<TapScreen> {
  int _selectPageIndex = 0;
  void _selectPage(int index) {
    setState(() {
      _selectPageIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("meal"),
      ),
      body: _pages[_selectPageIndex]['page'],
      bottomNavigationBar: BottomNavigationBar(
        onTap: _selectPage,
        backgroundColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.white,
        selectedItemColor: Colors.amber,
        currentIndex: _selectPageIndex,
        type: BottomNavigationBarType.shifting,
        items: [
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.star),
            label: 'Favorites',
          ),
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.category),
            label: 'Favorites',
          ),
        ],
      ),
    );
  }
}

2

Answers


  1. You can provide default widget on null case.

    body: _pages[_selectPageIndex]['page'] as Widget? ?? Text("not found"),
    
    Login or Signup to reply.
  2. In Dart Object can be anything and the body of the scaffold expects only the type Widget thus causing the error. It is always better to use Widget type annotation.

    In your case, Check if that Object is a widget type or not

     _pages[_selectPageIndex]['page'] is Widget ?  _pages[_selectPageIndex]['page'] as Widget : Text("Page not found");
    

    Full code

    class TapScreen extends StatefulWidget {
      const TapScreen({super.key});
    
      @override
      State<TapScreen> createState() => _TapScreenState();
    }
    
    List<Map<String, Object>> _pages = [
      {'page': CategoriesScreen(), 'title': 'Catergories'},
      {'page': FavoitesScreen(), 'title': 'your Favorites'}
    ];
    
    class _TapScreenState extends State<TapScreen> {
      int _selectPageIndex = 0;
      void _selectPage(int index) {
        setState(() {
          _selectPageIndex = index;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("meal"),
          ),
          body: _pages[_selectPageIndex]['page'] is Widget ? _pages[_selectPageIndex]['page'] as Widget : Text("Page not found"), // Change here
          bottomNavigationBar: BottomNavigationBar(
            onTap: _selectPage,
            backgroundColor: Theme.of(context).primaryColor,
            unselectedItemColor: Colors.white,
            selectedItemColor: Colors.amber,
            currentIndex: _selectPageIndex,
            type: BottomNavigationBarType.shifting,
            items: [
              BottomNavigationBarItem(
                backgroundColor: Theme.of(context).primaryColor,
                icon: Icon(Icons.star),
                label: 'Favorites',
              ),
              BottomNavigationBarItem(
                backgroundColor: Theme.of(context).primaryColor,
                icon: Icon(Icons.category),
                label: 'Favorites',
              ),
            ],
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search