'NoSuchMethod Error: Method called on null
//in this code i am tring to use connectivity. Android recommended me to add some classes, //getters setter, I added them at the end of the code automatically
import 'package:firebase_auth/firebase_auth.dart';[enter image description here][1]
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'signup.dart';
import 'home.dart';
import 'dart:async';
import 'dart:developer' as developer;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Login extends StatefulWidget {
const Login({Key? key}) : super(key: key);
@override
_LoginState createState() => _LoginState();
}
//loginstate
class _LoginState extends State<Login> {
ConnectivityResult? _connectionStatus = ConnectivityResult.none;
final Connectivity _connectivity = Connectivity();
late StreamSubscription<ConnectivityResult> _connectivitySubscription;
@override
void initState() {
super.initState();
initConnectivity();
_connectivitySubscription =
_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
}
@override
void dispose() {
_connectivitySubscription.cancel();
super.dispose();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initConnectivity() async {
late ConnectivityResult result;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
result = await _connectivity.checkConnectivity();
} on PlatformException catch (e) {
developer.log('Couldn\'t check connectivity status', error: e);
return;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) {
return Future.value(null);
}
return _updateConnectionStatus(result);
}
Future<void> _updateConnectionStatus(ConnectivityResult result) async {
setState(() {
_connectionStatus = result;
});
}
//Login function
static Future<User?> loiginUsingEmailPassword(
{required String email,
required String password,
required BuildContext context}) async {
FirebaseAuth auth = FirebaseAuth.instance;
User? user;
try {
UserCredential userCredential = await auth.signInWithEmailAndPassword(
email: email, password: password);
user = userCredential.user;
} on FirebaseAuthException catch (e) {
if (e.code == "user-not-found") {
print("No user found for that email");
}
}
return user;
}
@override
Widget build(BuildContext context) {
//create textfield controller
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
return Scaffold(
resizeToAvoidBottomInset: false,
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(15.0, 110.0, 0.0, 0.0),
child: Text(
'Bon Appétit',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 50.0,
),
),
),
//new container Container( padding: EdgeInsets.only(top: 35.0, left: 20.0, right: 20.0), child: Column( children: [ TextField( controller: _emailController, decoration: InputDecoration( focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Color(0xFFcaa052)), ), labelText: 'EMAIL', labelStyle: TextStyle( fontFamily: 'Alegreya Sans', fontWeight: FontWeight.bold, color: Color(0xFFcaa052), ), ), ), SizedBox( height: 20.0, ), TextField( controller: _passwordController, decoration: InputDecoration( focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Color(0xFFcaa052)), ), labelText: 'PASSWORD', labelStyle: TextStyle( fontFamily: 'Alegreya Sans', fontWeight: FontWeight.bold, color: Color(0xFFcaa052), ), ), obscureText: true, ), SizedBox( height: 5.0, ), Container( alignment: Alignment(1.0, 0.0), padding: EdgeInsets.only(top: 15.0, left: 20.0), child: InkWell( child: Text( 'Forgot Password', style: TextStyle( color: Color(0xFF5E0B0B), fontWeight: FontWeight.bold, fontFamily: 'Alegreya Sans', decoration: TextDecoration.underline, ), ), ), ), SizedBox( height: 40.0, ), ], ), ), SizedBox( height: 15.0, ), Container( height: 40.0, width: 400.0, child: Material( borderRadius: BorderRadius.circular(20.0), shadowColor: Color(0xFFffffb1), color: Color(0xFFffd180), elevation: 7.0, child: InkWell( onTap: () async { User? user = await loiginUsingEmailPassword( email: _emailController.text, password: _passwordController.text, context: context); print(user); if (user != null) { Navigator.push( context, MaterialPageRoute(builder: (context) => Home()), ); } }, child: Center( child: Text( 'LOGIN', style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontFamily: 'Alegreya Sans', ), ),
),
),
),
),
Container(
child: Text('Connection Status: ${_connectionStatus.toString()}'),
),
SizedBox(
height: 15.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'New member?',
style: TextStyle(fontFamily: 'Alegreya Sans'),
),
SizedBox(
width: 5.0,
),
InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Signup()),
);
},
child: Text(
'Sign up',
style: TextStyle(
color: Color(0xFF5E0B0B),
fontWeight: FontWeight.bold,
fontFamily: 'Alegreya Sans',
decoration: TextDecoration.underline,
),
),
),
],
),
],
),
),
);
}
}
//i think because of below code i got error
class ConnectivityResult {
static ConnectivityResult? none;
}
class Connectivity {
get onConnectivityChanged => null;
checkConnectivity() {}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
