Home > OS >  The instance member 'cacheManager' can't be accessed in an initializer. Try replacing
The instance member 'cacheManager' can't be accessed in an initializer. Try replacing

Time:12-14

I Want to if gettoken is null show login page but gettoken is not null show profile page(sorry for my english).Now i dont have a profile page so i put empty.i used list to show pages but I dont know without list.And i want to this desing.Because this is what he wants from me.I used sharedpreference for cacheManager.gettoken

import 'dart:io';

import 'package:animated_bottom_navigation_bar/animated_bottom_navigation_bar.dart';
import 'package:est/view/auth/view/login_view.dart';
import 'package:flutter/material.dart';
import '../../empty_page.dart';
import '../../product/constant/app_color.dart';
import '../../product/constant/app_text.dart';
import '../../product/user_check/cache_manager.dart';
import '../appo/view/appo_view.dart';
import '../home/Home.dart';

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

  @override
  State<DashBoardView> createState() => _DashBoardViewState();
}

class _DashBoardViewState extends State<DashBoardView> {
  CacheManager cacheManager = CacheManager();
  int _bottomNavIndex = 0;
  final List<IconData> _iconList = [Icons.home, Icons.person, Icons.local_hospital, Icons.car_crash];

  final pageList = [const Home(), const AppoView(), const Empty(),`this is error "cachemanager"`cacheManager.getToken()=="" ?const Empty():LoginView()];

  @override
  Widget build(BuildContext context) {
    bool showFab = MediaQuery.of(context).viewInsets.bottom != 0;

    return WillPopScope(
      onWillPop: () async {
        final value = await showDialog<bool>(
            context: context,
            builder: (context) {
              return _willPopScopeDialog(context);
            });
        if (value != null) {
          if (value == true) {
            exit(0);
          }
        }
        return false;
      },
      child: Scaffold(
        extendBody: true,
        body: pageList[_bottomNavIndex],
        floatingActionButton: Visibility(
          visible: !showFab,
          child: FloatingActionButton(
              child: const Text(
                " ",
                style: TextStyle(fontSize: 55),
              ),
              onPressed: () {
                showDialog(
                  context: context,
                  builder: (context) {
                    return Padding(
                      padding: const EdgeInsets.only(bottom: 100),
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [_floatingButton(myproc, const Empty()), _floatingButton(online, const Empty())]),
                    );
                  },
                );
              }),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        bottomNavigationBar: AnimatedBottomNavigationBar(
          inactiveColor: Colors.grey,
          activeColor: primaryColor1,
          gapLocation: GapLocation.center,
          icons: _iconList,
          notchSmoothness: NotchSmoothness.softEdge,
          activeIndex: _bottomNavIndex,
          onTap: (index) {
            setState(() {
              _bottomNavIndex = index;
            });
          },
        ),
      ),
    );
  }

  TextButton _floatingButton(String text, Widget pushpage) {
    return TextButton.icon(
        style: const ButtonStyle(backgroundColor: MaterialStatePropertyAll<Color>(Colors.white)),
        onPressed: () {
          Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => pushpage,
          ));
        },
        icon: const Icon(Icons.home, color: primaryColor1),
        label: Text(
          text,
          style: const TextStyle(fontFamily: poppins, color: Colors.black, fontSize: 18),
        ));
  }
}

AlertDialog _willPopScopeDialog(BuildContext context) {
  return AlertDialog(
    content: const Text(leave, style: TextStyle(fontFamily: poppins)),
    actions: [
      ElevatedButton(
          style: ElevatedButton.styleFrom(backgroundColor: primaryColor1),
          onPressed: () => Navigator.of(context).pop(true),
          child: const Text(yes, style: TextStyle(fontFamily: poppins))),
      ElevatedButton(
          style: ElevatedButton.styleFrom(backgroundColor: primaryColor1),
          onPressed: () => Navigator.of(context).pop(false),
          child: const Text(no, style: TextStyle(fontFamily: poppins)))
    ],
  );
}

CodePudding user response:

This is because you use a condition in a final member with a test in it. As a result you should have variable with late modifier.

 late pageList = [const Home(), const AppoView(), const Empty(),cacheManager.getToken()=="" ?const Empty():LoginView()];

Or

  late pageList = [];
  
  @override
  void initState() {
    super.initState();
    pageList = [const Home(), const AppoView(), const Empty(),cacheManager.getToken()=="" ?const Empty():LoginView()];
  }
  • Related