'"Homeviewmodel?" not found you need to call "Get.put(HomeViewModel?())" or "GET.lazyPut(() => HomeViewModel?()) ..,
i got this problem after i changed to null safety so whats problem ?
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following message was thrown building HomeView(dirty): "HomeViewModel?" not found. You need to call "Get.put(HomeViewModel?())" or "Get.lazyPut(()=>HomeViewModel?())"
The relevant error-causing widget was: HomeView HomeView:file:///C:/Users/coolalonge/Downloads/Ecommerce_App-master/lib/main.dart:62:15
When the exception was thrown, this was the stack: #0 GetInstance.find (package:get/get_instance/src/get_instance.dart:305:7) #1 Inst.find (package:get/get_instance/src/extension_instance.dart:69:45) #2 HomeView.build (package:ecommerce_app/view/home_view.dart:52:17) #3 StatelessElement.build (package:flutter/src/widgets/framework.dart:4827:28) #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15) #5 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5) #6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735:5) #7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729:5) ... Normal element mounting (19 frames) #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14) #27 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36) #28 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32) ... Normal element mounting (255 frames) #283 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14) #284 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36) #285 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32) ... Normal element mounting (388 frames) #673 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14) #674 Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18) #675 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16) #676 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5) #677 RenderObjectToWidgetAdapter.attachToRenderTree. (package:flutter/src/widgets/binding.dart:1112:18) #678 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2600:19) #679 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13) #680 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7) #681 WidgetsBinding.scheduleAttachRootWidget. (package:flutter/src/widgets/binding.dart:924:7) (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
my code :
main :
import 'package:ecommerce_app/backup/homeview.dart';
import 'package:ecommerce_app/core/view_model/control_view_model.dart';
import 'package:ecommerce_app/core/view_model/home_view_model.dart';
import 'package:ecommerce_app/helper/binding.dart';
import 'package:ecommerce_app/view/auth/send_view.dart';
import 'package:ecommerce_app/view/control_view.dart';
import 'package:ecommerce_app/view/home_view.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Get.lazyPut(()=>HomeViewModel());
return GetMaterialApp(
debugShowCheckedModeBanner: false,
initialBinding: Binding(),
home: Scaffold(
appBar: AppBar(backgroundColor: Color.fromRGBO(0, 131, 72, 1),
title: Image.network(
--
),
drawer: Drawer(child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text('Drawer Header'),
),
ListTile(
title: const Text('Item 1'),
onTap: () {
// Update the state of the app.
// ...
},
),
ListTile(
title: const Text('Item 2'),
onTap: () {
// Update the state of the app.
// ...
},
),
],
),
),
body: HomeView(),
),
theme: ThemeData(
fontFamily: 'SourceSans',
),
);
}
};
homeview :
List<String> images = [];
UserModel? model;
@override
Widget build(BuildContext context) {
if (model?.email != "[email protected]") {
return LoginView();
}
// Get.lazyPut(()=>HomeViewModel());
return GetBuilder<HomeViewModel>(
init: Get.find(),
builder: (controller) => controller.loading.value
? Center(child: CircularProgressIndicator())
: Scaffold(
body: SingleChildScrollView(
viewModel:
import 'package:ecommerce_app/core/view_model/auth_view_model.dart';
import 'package:ecommerce_app/core/view_model/control_view_model.dart';
import 'package:ecommerce_app/core/view_model/home_view_model.dart';
import 'package:ecommerce_app/view/auth/login_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class ControlView extends GetWidget<AuthViewModel> {
@override
Widget build(BuildContext context) {
// Get.lazyPut(()=>HomeViewModel());
return Obx(() {
if ((Get.find<AuthViewModel>().user == null)) {
return LoginView();
} else {
return GetBuilder<ControlViewModel>(
builder: (controller) => Scaffold(
body: controller.currentScreen,
bottomNavigationBar: bottomNavigationBar(),
),
);
}
});
}
Widget bottomNavigationBar() {
return GetBuilder<ControlViewModel>(
init: ControlViewModel(),
builder: (controller) => BottomNavigationBar(
items: [
BottomNavigationBarItem(
activeIcon: Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text("Explore"),
),
label: '',
icon: Padding(
padding: const EdgeInsets.only(top: 10),
child: Image.asset(
'assets/images/Icon_Explore.png',
fit: BoxFit.contain,
width: 20,
),
)),
BottomNavigationBarItem(
activeIcon: Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text("Cart"),
),
label: '',
icon: Padding(
padding: const EdgeInsets.only(top: 10),
child: Image.asset(
'assets/images/Icon_Cart.png',
fit: BoxFit.contain,
width: 20,
),
)),
BottomNavigationBarItem(
activeIcon: Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text("Account"),
),
label: '',
icon: Padding(
padding: const EdgeInsets.only(top: 10),
child: Image.asset(
'assets/images/Icon_User.png',
fit: BoxFit.contain,
width: 20,
),
)),
],
currentIndex: controller.navigatorValue,
onTap: (index) {
controller.changeSelectedValue(index);
},
elevation: 0,
selectedItemColor: Colors.black,
backgroundColor: Color.fromRGBO(0, 131, 72, 1),
),
);
}
}
authViewModel :
import 'package:ecommerce_app/core/service/firestore_user.dart';
import 'package:ecommerce_app/model/user_model.dart';
import 'package:ecommerce_app/view/home_view.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
import 'package:get/get.dart';
import 'package:google_sign_in/google_sign_in.dart';
class AuthViewModel extends GetxController {
// GoogleSignIn _googleSignIn = GoogleSignIn(scopes: ['email']);
FirebaseAuth _auth = FirebaseAuth.instance;
late String email, password, name;
Rxn<User> _user = Rxn<User>();
String? get user => _user.value?.email;
@override
void onInit() {
// TODO: implement onInit
super.onInit();
// _user.bindStream(_auth.authStateChanges());
}
@override
void onReady() {
// TODO: implement onReady
super.onReady();
}
@override
void onClose() {
// TODO: implement onClose
super.onClose();
}
// void googleSignInMethod() async {
// final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
// print(googleUser);
// GoogleSignInAuthentication? googleSignInAuthentication =
// await googleUser?.authentication;
// final AuthCredential credential = GoogleAuthProvider.credential(
// idToken: googleSignInAuthentication?.idToken,
// accessToken: googleSignInAuthentication?.accessToken,
// );
// await _auth.signInWithCredential(credential).then((user) {
// saveUser(user);
// Get.offAll(HomeView());
// });
// }
// void facebookSignInMethod() async {
// final AccessToken result = await FacebookAuth.instance.login();
// final FacebookAuthCredential facebookAuthCredential =
// FacebookAuthProvider.credential(result.token);
// await _auth.signInWithCredential(facebookAuthCredential).then((user) {
// saveUser(user);
// });
// }
void signInWithEmailAndPassword() async {
try {
await _auth.signInWithEmailAndPassword(email: email, password: password);
Get.offAll(HomeView());
} catch (e) {
print("e.message");
Get.snackbar(
'Error login account',
"e.message",
colorText: Colors.black,
snackPosition: SnackPosition.BOTTOM,
);
}
}
void createAccountWithEmailAndPassword() async {
try {
await _auth
.createUserWithEmailAndPassword(email: email, password: password)
.then((user) async {
saveUser(user);
});
Get.offAll(HomeView());
} catch (e) {
print("e.message");
Get.snackbar(
'Error login account',
" e.message",
colorText: Colors.black,
snackPosition: SnackPosition.BOTTOM,
);
}
}
void saveUser(UserCredential user) async {
await FireStoreUser().addUserToFireStore(UserModel(
userId: user.user?.uid,
email: user.user?.email,
name: name == null ? user.user!.displayName : name,
pic: '',
// role: '',
));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
