Search code examples
flutterdartflutter-layoutwidget

type 'Home Page' is not a subtype of type 'bool'


my app getting a type 'Home Page' is not a subtype of type 'bool' error.

My HomePage code

import 'package:flutter/cupertino.dart';

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

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

My Main code

import 'package:flutter/material.dart';
import 'package:medreminder/home_page.dart';
import 'package:medreminder/profile_page.dart';
import 'package:medreminder/settings_page.dart';

void main() => runApp(MainPage());

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

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  List pages = [
    HomePage(),
    SettingPage(),
    ProfilePage()
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height * 0.4, 
      ),
      extendBody: pages[0],
      bottomNavigationBar: BottomNavigationBar(
        selectedItemColor: Colors.black,
        items: [
          BottomNavigationBarItem(label: "Home", icon: Icon(Icons.home)),
          BottomNavigationBarItem(label: "Settings", icon: Icon(Icons.settings)),
          BottomNavigationBarItem(label: "Profile", icon: Icon(Icons.account_circle)),
          
        ],
      ),
    );
  }
}

Here is the full error

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building MainPage(dirty, dependencies: [MediaQuery], state:
_MainPageState#e8259):
type 'HomePage' is not a subtype of type 'bool'

The relevant error-causing widget was:
  MainPage
  MainPage:file:///D:/Kuliah/Mata%20Kuliah/Pemrograman%20Mobile/medicine_reminder/code/lib/main.dart:19:13

When the exception was thrown, this was the stack:
#0      _MainPageState.build (package:medreminder/main_page.dart:28:24)
#1      StatefulElement.build (package:flutter/src/widgets/framework.dart:4992:27)
#2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4878:15)
#3      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4859:5)
#6      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5041:11)
#7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4853:5)
...     Normal element mounting (275 frames)
#282    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#283    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6435:36)
#284    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6447:32)
...     Normal element mounting (407 frames)
#691    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#692    Element.updateChild (package:flutter/src/widgets/framework.dart:3592:18)
#693    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1195:16)
#694    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1164:5)
#695    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1111:18)        
#696    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2605:19)
#697    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1110:13)
#698    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:945:7)
#699    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:925:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════════════════════════

Solution

  • You need to pass a bool on extendBody

    it can be extendBody: true,

    Also you need to provide MaterialApp on top, it can be

    void main() => runApp(MaterialApp(home: MainPage()));
    
    class MainPage extends StatefulWidget {
      const MainPage({super.key});
    
      @override
      State<MainPage> createState() => _MainPageState();
    }
    
    class _MainPageState extends State<MainPage> {
      List<Widget> pages = [HomePage(), Text("s"), Text("ProfilePage")];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height * 0.4,
            child: pages[0],
          ),
          extendBody: true,
          bottomNavigationBar: BottomNavigationBar(
            selectedItemColor: Colors.black,
            items: [
              BottomNavigationBarItem(label: "Home", icon: Icon(Icons.home)),
              BottomNavigationBarItem(
                  label: "Settings", icon: Icon(Icons.settings)),
              BottomNavigationBarItem(
                  label: "Profile", icon: Icon(Icons.account_circle)),
            ],
          ),
        );
      }
    }