user23597482
user23597482

Reputation: 3

Flutter Provider issue

State management uses Provider, and the design pattern uses the MVVM pattern.

Simply put, the current issue is that there is a problem with the value being initialized when retrieving viewModel data using Provider.

  1. auth_viewModel > Account-related viewModel such as login, logout, etc.

     Future<void> login(String userid, String password) async {
        _gAuthMsg = '';
        _routeMsg = '';
    
        if (await _authRepository.isMember(userid, password)) {
          if (await _authRepository.isAuthUserInfo()) {
            await analyticsSettingUpUserId(userid);
            await openDynamicLink();
    
            _isNoteUser = await getAllNoteUserInfo(userid);
            _isRegistLongtermCare = await findRegistLongtermCare();
    
            if (_gNoteUserInfo == null || _noteUserList.isEmpty) {
              _routeMsg = 'goFacility';
              _isLogined = true;
            } else {
              if (_isNoteUser && _isRegistLongtermCare) {
                if (_gNoteUserInfo!.grant == 'true') {
                  // (imageViewModel & menuViewModel)'s get Data > issue!!
                  await imageViewModel.getAgencyBanner();
                  await imageViewModel.getAgencyImage();
    
                  await menuViewModel.getAgencyMenu();
                  await menuViewModel.getIndividualMenu();
    
                  _isLogined = true;
                } else {
                  if (!_isNoteUser) {
                    _gAuthMsg = 'emptyNoteUser';
                  } else {
                    _gAuthMsg = 'emptyAgency';
                  }
                  _isLogined = false;
    
                }
              } else {
                _isLogined = false;
              }
            }
          }
        }
    
    
        if (_isLogined) {
          LoginInfo info = LoginInfo(
            userid: userid,
            password: password,
            onsave: _onSave,
            agencynum: _gNoteUserInfo != null ? loginInfo.agencynum : '',
          );
          _authRepository.putLoginInfo(info);
        }
        notifyListeners();
      }
    
  2. loginView.dart

    onPressed: () async{
     await context.read<AuthViewModel>().login(userid, password);
    }
    

    After logging in, you will be moved to IndexPage.

  3. indexPage.dart

class IndexPage extends StatefulWidget {
  IndexPage({super.key, required this.route});

  Widget route;

  @override
  State<IndexPage> createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  // AuthViewModel? authViewModel;
  // MenuViewModel? menuViewModel;
  @override
  void initState() {
    // authViewModel = AuthViewModel();
    // menuViewModel = MenuViewModel();

    // context.read<MenuViewModel>().getAgencyMenu();
    // context.read<MenuViewModel>().getIndividualMenu();

    print('init');
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    print('didChange');
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    return Consumer<MenuViewModel>(
      builder: (context, value, child) {
        return Text('');
      },
    );
  }
}
  1. menu_viewModel.dart
class MenuViewModel extends ChangeNotifier {
  late final MenuRepository _menuRepository;

  List<MainMenu> _mainmenu = [];
  List<IndividualMainMenu> _individualMainmenu = [];

  List<MainMenu> get mainmenu => _mainmenu;
  List<IndividualMainMenu> get individualMainmenu => _individualMainmenu;

  MenuViewModel() {
    _menuRepository = MenuRepository();
  }

  Future<void> getAgencyMenu() async {
    _mainmenu = await _menuRepository.getAgencyMenu();
    notifyListeners();
  }

  Future<void> getIndividualMenu() async {
    _individualMainmenu = await _menuRepository.getIndividualMenu();
    notifyListeners();
  }
}

I created the IndexPage briefly as a StatefulWidget and tried using various methods to access each ViewModel.

There is a problem where the data that came out fine when debugging in the login() function is initialized in the IndexPage and an empty value appears.

It seems that the role of status management is not being fulfilled properly. What is the problem?

Oh, in addition, the part that I can't get is the MenuViewModel data.

Upvotes: 0

Views: 43

Answers (0)

Related Questions