bottomNavigationBar with dynamic menu in Flutter

277 views Asked by At

I try to bottomnavigatorbar with dynamic menu comes from Provider.

Its' work with error.

The following assertion was thrown building FutureBuilder(dirty, state: _FutureBuilderState#dbbbc): 'package:flutter/src/material/bottom_navigation_bar.dart': Failed assertion: line 186 pos 15: 'items.length >= 2': is not true.

class ChartPageViewer extends StatefulWidget {
  const ChartPageViewer({Key? key}) : super(key: key);

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

class _ChartPageViewerState extends State<ChartPageViewer> {
  double? yas;
  double? boy;
  String tablo = "neyzi";
  PageController pageController = PageController(initialPage: 0);
  int currentindex = 0;

  @override
  void initState() {
    yas = context.read<HastaVeriKaydet>().yas?.toDouble();
    boy = double.parse(context.read<HastaVeriKaydet>().boy!);
    tablo = context.read<HastaVeriKaydet>().tablo!;
    PersentilChartRepository().runServices(
        yas: yas != null ? yas! : 0,
        olcum: boy != null ? boy! : 0,
        olcumtipi: 'boy',
        cinsiyet: cinsiyet,
        tablo: tablo);
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      Provider.of<PersentilChartRepository>(context, listen: false)
          .addtabloListesi(
              tablo: tablo, cinsiyet: cinsiyet, yas: yas.toString());
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    List<String>? menuListesi =
        Provider.of<PersentilChartRepository>(context).tabloListesi != null
            ? Provider.of<PersentilChartRepository>(context).tabloListesi
            : null;
    if (tablo == 'cdc' && yas! >= 36) {
      menuListesi!.remove('bascevresi');
    }
    if (tablo == 'who' && yas! >= 120) {
      menuListesi!.remove('kilo');
    }
    StatefulWidget menuEslestir(String menu) {
      switch (menu) {
        case 'boy':
          return HeightChart();
        case 'kilo':
          return WeightChart();
        case 'bascevresi':
          return Bascevresi();
        case 'bmi':
          return Bmi();
        case 'boyagorekilo':
          return Boyagorekilo();
        default:
          return HeightChart();
      }
    }

    List<BottomNavigationBarItem> barItem() {
      return menuListesi!.map((e) {
        Widget itemicon = Icon(Icons.star);
        if (e == 'boy') {
          itemicon = Icon(Icons.home);
        } else if (e == 'kilo') {
          itemicon = Icon(Icons.opacity);
        } else if (e == 'bascevresi') {
          itemicon = Icon(Icons.map);
        } else if (e == 'bmi') {
          itemicon = Icon(Icons.close);
        } else if (e == 'boyagorekilo') {
          itemicon = Icon(Icons.search);
        }
        return BottomNavigationBarItem(icon: itemicon, label: e);
      }).toList();
    }

    return Scaffold(
      bottomNavigationBar: FutureBuilder(
        future: Future.delayed(Duration(milliseconds: 100), () {
          // Do something
        }),
        builder: (context, snapshot) {
          return BottomNavigationBar(
            onTap: (index) {
              pageController.animateToPage(index,
                  duration: Duration(milliseconds: 100), curve: Curves.linear);
            },
            backgroundColor: Colors.blueGrey,
            selectedItemColor: Colors.black,
            unselectedItemColor: Colors.grey,
            currentIndex: currentindex,
            items: barItem(),
          );
        },
      ),
      body: PageView.builder(
        itemCount: menuListesi!.length,
        controller: pageController,
        pageSnapping: true,
        onPageChanged: (index) {
          setState(() {
            currentindex = index;
          });
        },
        itemBuilder: (context, i) {
          return menuEslestir(menuListesi[i]);
        },
      ),
    );
  }
}

It's work on screen but there is error run screen. I don't want any problem.

If i put print(baritem().lenght) in navigatorbar bloc I can see three times length in console First lenght value is 0 ; other value is real number.

0

There are 0 answers