Related
I am developing a flutter web app in which I have developed custom navbar but I am struggling to add navigation in the navbar items. I've tried Get to navigate without context but unfortunately it isn't working on the list I've created. I did tried to create function and put navigation as required for it but still no luck.
import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:shimmer/shimmer.dart';
import 'package:customweb/about.dart';
double collapsableHeight = 0.0;
Color selected = Color(0xffffffff);
Color notSelected = Color(0xafffffff);
class Nav extends StatefulWidget {
#override
_NavState createState() => _NavState();
}
class _NavState extends State<Nav> {
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return SafeArea(
child: Scaffold(
body: Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.only(top: 250),
child: Image.asset(
"assets/logo.png",
scale: 2,
)),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: EdgeInsets.only(bottom: 120),
child: Text(
"Download now available on",
style: TextStyle(fontSize: 30),
))),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: EdgeInsets.only(right: 200, bottom: 50),
child: Image.asset(
"assets/1200px-Google_Play_Store_badge_EN.png",
scale: 8,
))),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: EdgeInsets.only(left: 200, bottom: 50),
child: Image.asset(
"assets/1200px-Download_on_the_App_Store_Badge.png",
scale: 8,
))),
AnimatedContainer(
margin: EdgeInsets.only(top: 79.0),
duration: Duration(milliseconds: 375),
curve: Curves.ease,
height: (width < 800.0) ? collapsableHeight : 0.0,
width: double.infinity,
color: Color(0xff121212),
child: SingleChildScrollView(
child: Column(
children: navBarItems,
),
),
),
Container(
color: Colors.amber,
height: 80.0,
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
AnimatedTextKit(
animatedTexts: [
FadeAnimatedText(
'Get It',
textStyle: TextStyle(fontSize: 26, color: Colors.white),
),
FadeAnimatedText(
'All Amazing Stuff',
textStyle: TextStyle(fontSize: 22, color: Colors.white),
),
FadeAnimatedText(
'One Platform',
textStyle: TextStyle(fontSize: 24, color: Colors.white),
),
FadeAnimatedText(
'Endless Services',
textStyle: TextStyle(fontSize: 24, color: Colors.white),
),
FadeAnimatedText(
'Available Soon',
textStyle: TextStyle(fontSize: 24, color: Colors.white),
),
],
onTap: () {
print("");
},
),
LayoutBuilder(builder: (context, constraints) {
if (width < 800.0) {
return NavBarButton(
onPressed: () {
if (collapsableHeight == 0.0) {
setState(() {
collapsableHeight = 240.0;
});
} else if (collapsableHeight == 240.0) {
setState(() {
collapsableHeight = 0.0;
});
}
},
);
} else {
return Row(
children: navBarItems,
);
}
})
],
),
),
],
),
),
);
}
}
List<Widget> navBarItems = [
NavBarItem(
text: 'About',
onPressed: () {
Get.to(AboutPage());
},
),
NavBarItem(
text: 'Services',
onPressed: () {
Get.to(AboutPage());
},
),
NavBarItem(
text: 'FAQ',
onPressed: () {
Get.to(AboutPage());
},
),
];
class NavBarItem extends StatefulWidget {
final String text;
final Function onPressed;
NavBarItem({
required this.text,
required this.onPressed,
});
#override
_NavBarItemState createState() => _NavBarItemState();
}
class _NavBarItemState extends State<NavBarItem> {
Color color = notSelected;
#override
Widget build(BuildContext context) {
return MouseRegion(
onEnter: (value) {
setState(() {
color = selected;
});
},
onExit: (value) {
setState(() {
color = notSelected;
});
},
child: Material(
color: Colors.transparent,
child: InkWell(
splashColor: Colors.white60,
onTap: () {},
child: Container(
height: 60.0,
alignment: Alignment.centerLeft,
margin: EdgeInsets.symmetric(horizontal: 24.0),
child: Text(
widget.text,
style: TextStyle(
fontSize: 16.0,
color: color,
),
),
),
),
),
);
}
}
class NavBarButton extends StatefulWidget {
final Function onPressed;
NavBarButton({
required this.onPressed,
});
#override
_NavBarButtonState createState() => _NavBarButtonState();
}
class _NavBarButtonState extends State<NavBarButton> {
Widget build(BuildContext context) {
return Container(
height: 55.0,
width: 60.0,
decoration: BoxDecoration(
border: Border.all(
color: Color(0xcfffffff),
width: 2.0,
),
borderRadius: BorderRadius.circular(15.0),
),
child: Material(
color: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
child: InkWell(
splashColor: Colors.white60,
onTap: () {
setState(() {
widget.onPressed();
});
},
child: Icon(
Icons.menu,
size: 30.0,
color: Color(0xcfffffff),
),
),
),
);
}
}
Any help will be highly appreciated
Hi Asver Seb you are following wrong method. I've recently done a job on flutter web here use my code of navbar/header to build what you are looking for it will do perfectly fine.
class Header extends StatelessWidget {
const Header({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: 20, horizontal: 40),
child: Row(
children: <Widget>[
Image.asset(
'assets/images/logo.png',
width: 50,
),
SizedBox(width: 10),
Shimmer.fromColors(
baseColor: Colors.transparent,
highlightColor: Colors.amber,
child: Text(
"Webster",
style: TextStyle(fontSize: 32, fontWeight: FontWeight.w800),
)),
Spacer(),
NavItem(
title: 'Home',
tapEvent: () {
Get.to(HomeScreen());
},
),
NavItem(
title: 'About',
tapEvent: () {
Get.to(About());
},
),
NavItem(
title: 'FAQ',
tapEvent: () {
Get.to(Faq());
},
),
],
),
);
}
}
class NavItem extends StatelessWidget {
const NavItem({Key? key, required this.title, required this.tapEvent})
: super(key: key);
final String title;
final GestureTapCallback tapEvent;
#override
Widget build(BuildContext context) {
return InkWell(
onTap: tapEvent,
hoverColor: Colors.transparent,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: Text(
title,
style: TextStyle(fontWeight: FontWeight.w300),
),
),
);
}
}
I'm using GetX to get the path of Image after the image is cropped and storing it in an RXString on one page:
RxString imageAdd = '$imagePath'.obs;
Then I'm retrieving the Path of the image on another page using:
Obx(
() => Image(
image: AssetImage(
(pollImageController.imageAdd.value)),
But the image is not displaying on the second page.
Complete Code:
ImagePickerCropper Page:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:justpoll/Constants.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:justpoll/screens/create_poll/create_poll4.dart';
String imagePath;
class CreatePoll3 extends StatefulWidget {
#override
_CreatePoll3State createState() => _CreatePoll3State();
}
class _CreatePoll3State extends State<CreatePoll3> {
final controller = Get.put(PollImageController());
File _selectedFile;
bool _inProcess = false;
final _picker = ImagePicker();
Widget getImageWidget() {
if (_selectedFile != null) {
return Image.file(
_selectedFile,
width: 370,
height: 230,
fit: BoxFit.cover,
);
} else {
return Padding(
padding: const EdgeInsets.only(bottom: 20),
child: Image.asset(
"assets/Images/placeholder.png",
width: 370,
height: 230,
fit: BoxFit.cover,
),
);
}
}
getImage(ImageSource source) async {
this.setState(() {
_inProcess = true;
});
PickedFile image = await _picker.getImage(source: source);
if (image != null) {
File cropped = await ImageCropper.cropImage(
sourcePath: image.path,
aspectRatio: CropAspectRatio(ratioX: 2, ratioY: 1.5),
compressQuality: 100,
maxWidth: 700,
maxHeight: 700,
compressFormat: ImageCompressFormat.jpg,
androidUiSettings: AndroidUiSettings(
toolbarColor: Colors.white,
toolbarTitle: "Crop Image",
statusBarColor: Colors.black,
backgroundColor: Colors.white,
));
imagePath = cropped.path;
print(imagePath);
this.setState(() {
_selectedFile = cropped;
_inProcess = false;
});
} else {
this.setState(() {
_inProcess = false;
});
}
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: MyColors.white,
appBar: AppBar(
title: Padding(
padding: const EdgeInsets.all(75.0),
child: Text('New Poll'),
),
leading: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back,
),
),
backgroundColor: Colors.black87,
),
body: ListView(
children: [
Column(
children: [
Center(
child: Padding(
padding: const EdgeInsets.only(top: 18, bottom: 28),
child: Text("3/4"),
),
),
// Padding(
// padding: const EdgeInsets.only(right: 230.0, top: 30, bottom: 30),
// child: Text(
// "Add Image",
// style: TextType.regularDarkText,
// ),
// ),
getImageWidget(),
Padding(
padding: const EdgeInsets.only(top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
MaterialButton(
onPressed: () {
getImage(ImageSource.camera);
},
color: Colors.black,
textColor: Colors.white,
child: Icon(
Icons.camera,
size: 24,
),
padding: EdgeInsets.all(16),
shape: CircleBorder(),
),
MaterialButton(
onPressed: () {
getImage(ImageSource.gallery);
},
color: Colors.black,
textColor: Colors.white,
child: Icon(
Icons.photo_album,
size: 24,
),
padding: EdgeInsets.all(16),
shape: CircleBorder(),
),
Align(
alignment: Alignment.bottomRight,
child: MaterialButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CreatePoll4(),
),
);
},
color: Colors.black,
textColor: Colors.white,
child: Icon(
Icons.arrow_forward,
size: 24,
),
padding: EdgeInsets.all(16),
shape: CircleBorder(),
),
)
],
),
),
(_inProcess)
? Container(
color: Colors.white,
height: MediaQuery.of(context).size.height * 0.95,
child: Center(
child: CircularProgressIndicator(),
),
)
: Center()
],
),
],
),
),
);
}
}
class PollImageController extends GetxController {
RxString imageAdd = '$imagePath'.obs;
// adding .obs makes this a stream based observable String
// this onInit override can be used instead of initState in a stateful widget.
// All text editing controllers can be initialized here
#override
void onInit() {
super.onInit();
}
#override
void onClose() {
super.onClose();
}
}
Image Display Page:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:justpoll/Constants.dart';
import 'package:justpoll/screens/home_page/nav.dart';
import 'create_poll1_static.dart';
import 'create_poll3.dart';
final pollDataController = Get.find<PollDataController>();
final pollImageController = Get.find<PollImageController>();
// final _questionController = pollDataController.questionController;
class CreatePoll4 extends StatefulWidget {
#override
_CreatePoll4State createState() => _CreatePoll4State();
}
class _CreatePoll4State extends State<CreatePoll4> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: MyColors.white,
appBar: AppBar(
title: Padding(
padding: const EdgeInsets.all(75.0),
child: Text('Poll Preview'),
),
leading: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back,
),
),
backgroundColor: Colors.black87,
),
body: ListView(
children: [
Column(
children: [
Center(
child: Padding(
padding: const EdgeInsets.only(top: 18, bottom: 28),
child: Text("4/4"),
),
),
Padding(
padding: const EdgeInsets.only(right: 240, top: 10),
child: Text(
'Select Theme',
style: TextType.regularDarkText,
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Row(
children: [
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.yellow,
textColor: Colors.white,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.orange,
textColor: Colors.white,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.brown,
textColor: Colors.white,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.blue,
textColor: Colors.white,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.green,
textColor: Colors.white,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.black,
textColor: Colors.white,
shape: CircleBorder(),
),
),
],
),
),
Card(
child: Container(
color: Colors.blue,
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Column(
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
backgroundImage:
AssetImage('assets/Images/blackLogo.png'),
),
),
Padding(
padding: const EdgeInsets.only(left: 10),
child: Text('JustPoll'),
),
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Obx(
() => Text(pollDataController.question.value),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Obx(
() => Image(
image: AssetImage(
(pollImageController.imageAdd.value)),
height: 150,
width: 200,
),
),
),
Row(
children: [
Padding(
padding:
const EdgeInsets.only(left: 120, top: 20),
child: Obx(
() => Text(pollDataController.op1.value),
),
),
SizedBox(
width: 70,
),
Padding(
padding:
const EdgeInsets.only(left: 10, top: 20),
child: Obx(
() => Text(pollDataController.op1Emoji.value),
),
),
],
),
Row(
children: [
Padding(
padding:
const EdgeInsets.only(left: 120, top: 20),
child: Obx(
() => Text(pollDataController.op2.value),
),
),
SizedBox(
width: 70,
),
Padding(
padding:
const EdgeInsets.only(left: 10, top: 20),
child: Obx(
() => Text(pollDataController.op2Emoji.value),
),
),
],
),
Row(
children: [
Padding(
padding:
const EdgeInsets.only(left: 120, top: 20),
child: Obx(
() => Text(pollDataController.op3.value),
),
),
SizedBox(
width: 70,
),
Padding(
padding:
const EdgeInsets.only(left: 10, top: 20),
child: Obx(
() => Text(pollDataController.op3Emoji.value),
),
),
],
),
Row(
children: [
Padding(
padding:
const EdgeInsets.only(left: 120, top: 20),
child: Obx(
() => Text(pollDataController.op4.value),
),
),
SizedBox(
width: 70,
),
Padding(
padding:
const EdgeInsets.only(left: 10, top: 20),
child: Obx(
() => Text(pollDataController.op4Emoji.value),
),
),
],
),
],
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Align(
alignment: Alignment.bottomRight,
child: MaterialButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Nav(),
),
);
},
color: Colors.black,
textColor: Colors.white,
child: Icon(
Icons.done,
size: 24,
),
padding: EdgeInsets.all(16),
shape: CircleBorder(),
),
),
)
],
),
],
),
),
);
}
}
I have tried all the methods to sign out from firebase auth. I am using Phone number auth in the Flutter app using Firebase but am unable to sign out the user. Every time I restart the app it leads me to the Home page but when I click the Logout button it takes me to the Login page.
What I want is a simple sign-out.
Authentication class
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:food_express/pages/login_signup/login.dart';
class Authentication{
Future<void> logOut(BuildContext context) async {
try {
await FirebaseAuth.instance.signOut().then((value) => {
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => Login()), (route) => false)
});
} catch (e) {
print(e); // TODO: show dialog with error
}
}
}
Profile Widget
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:food_express/constant/constant.dart';
import 'package:food_express/functions/Authentication.dart';
import 'package:food_express/pages/login_signup/login.dart';
import 'package:food_express/pages/order/history_order/history.dart';
import 'package:food_express/pages/order/order.dart';
import 'package:food_express/pages/profile/edit_profile.dart';
import 'package:page_transition/page_transition.dart';
import 'package:food_express/pages/profile/address.dart';
import 'package:food_express/pages/notification.dart';
import "../../constant/globals.dart" as global;
class Profile extends StatelessWidget {
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
logoutDialogue() {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
// return object of type Dialog
return Dialog(
elevation: 0.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
child: Container(
height: 130.0,
padding: EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"You sure want to logout?",
style: headingStyle,
),
SizedBox(
height: 20.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
InkWell(
onTap: () {
logout(context);
},
child: Container(
width: (width / 3.5),
alignment: Alignment.center,
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(5.0),
),
child: Text(
'Cancel',
style: buttonBlackTextStyle,
),
),
),
InkWell(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => Login()));
},
child: Container(
width: (width / 3.5),
alignment: Alignment.center,
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: primaryColor,
borderRadius: BorderRadius.circular(5.0),
),
child: Text(
'Log out',
style: wbuttonWhiteTextStyle,
),
),
),
],
),
],
),
),
);
},
);
}
return Scaffold(
backgroundColor: scaffoldBgColor,
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: whiteColor,
elevation: 0.0,
title: Text(
'Profile',
style: bigHeadingStyle,
),
),
body: ListView(
children: <Widget>[
InkWell(
onTap: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
child: EditProfile()));
},
child: Container(
width: width,
padding: EdgeInsets.all(fixPadding),
color: whiteColor,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
width: 70.0,
height: 70.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
image: DecorationImage(
image: NetworkImage(
"https://png.pngtree.com/png-clipart/20200224/original/pngtree-cartoon-color-simple-male-avatar-png-image_5230557.jpg"),
fit: BoxFit.cover,
),
),
),
widthSpace,
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
global.profile.Name.toString(),
style: headingStyle,
),
heightSpace,
Text(
global.profile.PhoneNumber.toString(),
style: lightGreyStyle,
),
],
),
],
),
Icon(
Icons.arrow_forward_ios,
size: 16.0,
color: Colors.grey.withOpacity(0.6),
),
],
),
),
),
Container(
margin: EdgeInsets.all(fixPadding),
padding: EdgeInsets.all(fixPadding),
decoration: BoxDecoration(
color: whiteColor,
borderRadius: BorderRadius.circular(5.0),
boxShadow: <BoxShadow>[
BoxShadow(
blurRadius: 1.5,
spreadRadius: 1.5,
color: Colors.grey[200],
),
],
),
child: Column(
children: <Widget>[
InkWell(
onTap: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
child: Notifications()));
},
child: getTile(
Icon(Icons.notifications,
color: Colors.grey.withOpacity(0.6)),
'Notifications'),
),
/* InkWell(
onTap: () {
Navigator.push(context, PageTransition(type: PageTransitionType.rightToLeft, child: PaymentMethods()));
},
child: getTile(
Icon(Icons.payment, color: Colors.grey.withOpacity(0.6)),
'Payment Methods'),
),
*/
/*InkWell(
onTap: () {},
child: getTile(
Icon(Icons.local_activity,
color: Colors.grey.withOpacity(0.6)),
'My Vouchers'),
),
*/
InkWell(
onTap: () {},
child: getTile(
Icon(Icons.group_add,
color: Colors.grey.withOpacity(0.6)),
'Invite Friends'),
),
],
),
),
Container(
margin: EdgeInsets.all(fixPadding),
padding: EdgeInsets.all(fixPadding),
decoration: BoxDecoration(
color: whiteColor,
borderRadius: BorderRadius.circular(5.0),
boxShadow: <BoxShadow>[
BoxShadow(
blurRadius: 1.5,
spreadRadius: 1.5,
color: Colors.grey[200],
),
],
),
child: Column(
children: <Widget>[
InkWell(
onTap: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
child: Order()));
},
child: getTile(
Icon(Icons.history, color: Colors.grey.withOpacity(0.6)),
'Order History'),
),
InkWell(
onTap: () {},
child: getTile(
Icon(Icons.group, color: Colors.grey.withOpacity(0.6)),
'Become our partner'),
),
InkWell(
onTap: () {},
child: getTile(
Icon(Icons.headset_mic,
color: Colors.grey.withOpacity(0.6)),
'Support'),
),
InkWell(
onTap: () {},
child: getTile(
Icon(Icons.star_rate,
color: Colors.yellow.withOpacity(0.6)),
'Rate our App'),
),
],
),
),
Container(
margin: EdgeInsets.all(fixPadding),
padding: EdgeInsets.all(fixPadding),
decoration: BoxDecoration(
color: whiteColor,
borderRadius: BorderRadius.circular(5.0),
boxShadow: <BoxShadow>[
BoxShadow(
blurRadius: 1.5,
spreadRadius: 1.5,
color: Colors.grey[200],
),
],
),
child: Column(
children: <Widget>[
InkWell(
onTap: logoutDialogue,
child: getTile(
Icon(Icons.exit_to_app,
color: Colors.grey.withOpacity(0.6)),
'Logout'),
),
],
),
),
],
),
);
}
getTile(Icon icon, String title) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
height: 40.0,
width: 40.0,
alignment: Alignment.center,
child: icon,
),
widthSpace,
Text(
title,
style: listItemTitleStyle,
),
],
),
Icon(
Icons.arrow_forward_ios,
size: 16.0,
color: Colors.grey.withOpacity(0.6),
),
],
);
}
void logout(context) {
Authentication _auth = Authentication();
_auth.logOut(context);
// await FirebaseAuth.instance.signOut();
// Navigator.pop(context);
}
}
Check the current user already log or not when log out, and when you use then block, you don't need to use the await keyword.
Future<void> logOut(BuildContext context) async {
try {
final User firebaseUser = await FirebaseAuth.instance.currentUser;
if (firebaseUser != null) {
FirebaseAuth.instance.signOut().then((value) => {
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context)
=> Login()), (route) => false)
});
}
} catch (e) {
print(e); // TODO: show dialog with error
}
}
Everytime when I try to use loggegInUser property it returns null please help me to solve this issue
getCurrent() methods works properly and prints user email and password but I can not acces this variables in Build Widget.
class _ProfilePageState extends State<ProfilePage> {
final _auth = FirebaseAuth.instance;
FirebaseUser loggedInUser;
String uid;
var url;
var userProfilePhotoUrl;
int currentPageIndex = 2;
CircularBottomNavigationController _navigationController =
new CircularBottomNavigationController(2);
List<TabItem> tabItems = List.of([
new TabItem(Icons.home, "Home", Colors.blue,
labelStyle: TextStyle(fontWeight: FontWeight.normal)),
new TabItem(Icons.add, "Let's Party", Colors.orange,
labelStyle: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
new TabItem(Icons.person, "Reports", Colors.red),
]);
#override
void initState() {
super.initState();
getCurrentUser();
}
void getCurrentUser() async {
try {
final user = await _auth.currentUser();
if (user != null) {
loggedInUser = user;
uid = user.uid;
print(loggedInUser.email);
print(uid);
}
} catch (e) {
print(e);
}
}
void changePage(int index) {
setState(() {
currentPageIndex = index;
});
}
Future<FirebaseImage> getFirebaseProfilPhoto() async {
try {
final user = await _auth.currentUser();
if (user != null) {
loggedInUser = user;
uid = user.uid;
print(loggedInUser.email);
print(uid);
return FirebaseImage(
'gs://homeparty-68792.appspot.com/user_profile_images/${loggedInUser.uid}.png');
}
} catch (e) {
print(e);
}
}
#override
Widget build(BuildContext context) {
setState(() {
getCurrentUser();
});
return Scaffold(
appBar: AppBar(
title: Text('furkanakguun'),
),
body: Builder(
builder: (context) => Container(
color: Colors.black,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 20.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.black,
backgroundImage: FirebaseImage(
'gs://homeparty-68792.appspot.com/user_profile_images/${loggedInUser.uid}.png'),
// FirebaseImage(
// 'gs://homeparty-68792.appspot.com/user_profile_images/$uid.png'),
//backgroundImage: Image.network(userProfilePhotoUrl),
radius: 100,
),
Padding(
padding: EdgeInsets.only(top: 60.0),
child: IconButton(
icon: Icon(
FontAwesomeIcons.camera,
size: 30.0,
),
focusColor: Colors.white,
color: Colors.deepPurple,
onPressed: () {
print('asd');
Navigator.pushNamed(context, 'image_uploader');
},
),
),
],
),
Divider(),
SizedBox(
height: 20.0,
),
Row(
children: <Widget>[
Card(
color: Colors.grey[900],
margin: EdgeInsets.symmetric(horizontal: 10.0),
child: Icon(Icons.person),
),
Card(
color: Colors.grey[900],
margin:
EdgeInsets.symmetric(horizontal: 10.0, vertical: 7.0),
child: Container(
child: Column(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Text('Furkan Akgün',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.bold)),
),
],
),
),
),
],
),
SizedBox(
height: 20.0,
),
Row(
children: <Widget>[
Card(
color: Colors.grey[900],
margin: EdgeInsets.symmetric(horizontal: 10.0),
child: Icon(Icons.location_on),
),
Card(
color: Colors.grey[900],
margin:
EdgeInsets.symmetric(horizontal: 10.0, vertical: 7.0),
child: Container(
child: Column(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Text('Ankara',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.bold)),
),
],
),
),
),
],
),
SizedBox(
height: 20.0,
),
Row(
children: <Widget>[
Card(
color: Colors.grey[900],
margin: EdgeInsets.symmetric(horizontal: 10.0),
child: Icon(Icons.rate_review),
),
Card(
color: Colors.grey[900],
margin:
EdgeInsets.symmetric(horizontal: 10.0, vertical: 7.0),
child: Container(
child: SmoothStarRating(
spacing: 2.0,
allowHalfRating: true,
starCount: 5,
rating: 4.5,
size: 20,
color: Colors.yellow,
borderColor: Colors.white,
isReadOnly: true,
)),
),
],
),
SizedBox(
height: 20.0,
),
SizedBox(
height: 40.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
color: Colors.deepPurple[900],
onPressed: () {
//uploadPic(context);
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: Text(
'Edit',
style: TextStyle(color: Colors.white, fontSize: 16.0),
),
),
],
)
],
),
),
),
bottomNavigationBar: CircularBottomNavigation(
tabItems,
barBackgroundColor: Colors.deepPurple[900],
controller: _navigationController,
selectedCallback: (int selectedPos) {
if (selectedPos == 0) {
Navigator.pushNamed(context, 'dashboard_screen');
}
if (selectedPos == 2) {
Navigator.pushNamed(context, 'user_profile');
}
},
),
);
}
}
enter image description here
ERROR TYPE
Exception has occurred.
NoSuchMethodError (NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null
Tried calling: uid)
My guess is that the error comes from this code in your build method:
FirebaseImage('gs://homeparty-68792.appspot.com/user_profile_images/${loggedInUser.uid}.png')
This is because the UI may be built before the user sign-in is completed.
To prevent this from happening you need to handle the fact that the user may not be signed in, in your build code too, for example by skipping the rendering of their profile image:
backgroundImage: loggedInUser != null ?
FirebaseImage('gs://homeparty-68792.appspot.com/user_profile_images/${loggedInUser.uid}.png') :
Text("Loading...")
Hi so I was doing an app like uber but I got an error of this. I'm new in using flutter. Before this, I can run app this normally but suddenly I got this error this morning.
This is my code. Can someone help me?
this is the error
A build function returned null.
The offending widget is: StreamBuilder
To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".
import 'package:ezbin3/Aboutus.dart';
import 'package:ezbin3/SCREENS/home/map.dart';
import 'package:ezbin3/SCREENS/home/map2.dart';
import 'package:ezbin3/SERVICES/auth.dart';
import 'package:ezbin3/activity.dart';
import 'package:ezbin3/models/user.dart';
import 'package:ezbin3/profile.dart';
import 'package:ezbin3/settings.dart';
import 'package:flutter/material.dart';
import 'package:ezbin3/SERVICES/database.dart';
import 'package:provider/provider.dart';
import 'package:ezbin3/models/ezuser.dart';
class Home extends StatelessWidget {
final AuthService _auth = AuthService();
#override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return StreamBuilder<EzuserData>(
stream: DatabaseService(uid: user.uid).userData,
builder: (context, snapshot) {
if (snapshot.hasData) {
EzuserData userData = snapshot.data;
return StreamProvider<List<Ezuser>>.value(
value: DatabaseService().userz,
child: Scaffold(
drawer: Drawer(
child: ListView(
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
gradient: LinearGradient(colors: <Color>[
Colors.lightGreen,
Colors.green
])),
child: Container(
child: Column(
children: <Widget>[
Material(
borderRadius:
BorderRadius.all(Radius.circular(50.0)),
elevation: 10,
child: Padding(
padding: EdgeInsets.all(8.0),
child: Image.asset(
'image/ezbin2.png',
width: 80,
height: 80,
),
),
),
SizedBox(height: 10),
Text(
userData.name,
style: TextStyle(
color: Colors.white, fontSize: 20.0),
),
],
)),
),
//PROFILE PAGE
Padding(
padding: const EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade400))),
child: InkWell(
splashColor: Colors.lightGreenAccent,
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProfilePage())),
child: Container(
height: 50,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(Icons.person),
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text('Profile',
style: TextStyle(
fontSize: 16.0))),
],
),
Icon(Icons.arrow_right)
],
),
)),
)),
//ACTIVITY PAGE
Padding(
padding: const EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade400))),
child: InkWell(
splashColor: Colors.lightGreenAccent,
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ActivityPage())),
child: Container(
height: 50,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(Icons.description),
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text('Activity',
style: TextStyle(
fontSize: 16.0))),
],
),
Icon(Icons.arrow_right)
],
),
)),
)),
//SETTING PAGE
Padding(
padding: const EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade400))),
child: InkWell(
splashColor: Colors.lightGreenAccent,
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SettingPage())),
child: Container(
height: 50,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(Icons.settings),
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text('Settings',
style: TextStyle(
fontSize: 16.0))),
],
),
Icon(Icons.arrow_right)
],
),
)),
)),
//ABOUT US PAGE
Padding(
padding: const EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade400))),
child: InkWell(
splashColor: Colors.lightGreenAccent,
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AboutusPage())),
child: Container(
height: 50,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(Icons.info_outline),
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text('About Us',
style: TextStyle(
fontSize: 16.0))),
],
),
Icon(Icons.arrow_right)
],
),
)),
)),
//LOG OUT
Padding(
padding: const EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade400))),
child: InkWell(
splashColor: Colors.lightGreenAccent,
onTap: () async {
await _auth.signOut();
print('logout');
},
child: Container(
height: 50,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(Icons.lock),
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text('Log Out',
style: TextStyle(
fontSize: 16.0))),
],
),
Icon(Icons.arrow_right)
],
),
)),
))
],
),
),
backgroundColor: Colors.green[50],
appBar: AppBar(
title: Text('EZBIN'),
backgroundColor: Colors.green[400],
elevation: 0.0,
actions: <Widget>[],
),
body: //HomeGoogle(),
MapSample(),
),
);
}
});
}
}
If anything happens please let me know. Thank you
Let's take a look at the error message
A build function returned null. The offending widget is: StreamBuilder
That means, that somewhere in your program a StreamBuilder (or its build method) is not returning a Widget, but null.
Now let's take a look at your code.
return StreamBuilder<EzuserData>(
stream: DatabaseService(uid: user.uid).userData,
builder: (context, snapshot) {
if (snapshot.hasData) {
EzuserData userData = snapshot.data;
return StreamProvider<List<Ezuser>>.value(
...
);
}
});
}
With the error and the simplified code in mind, where could you not return a Widget, but instead no value at all? Hint: What happens, if snapshot.hasData is not true?