【发布时间】:2021-12-01 13:35:56
【问题描述】:
我正在尝试从实时数据库中获取一个值并将其与用户提供的输入相匹配。如果匹配,则导航到下一页,否则抛出错误,但在我正在实现从实时获取价值的方法的部分,我收到以下错误:
E/flutter (17592): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常: NoSuchMethodError: 方法 '[]' 在 null 上调用。 E/颤振(17592):接收器:空 E/flutter (17592):尝试调用:
import 'package:country_code_picker/country_code_picker.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'OTPController.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key key}) : super(key: key);
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
DataSnapshot snapshot;
Query _ref;
String dialCodeDigits = "+977";
TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 100),
Padding(
padding: const EdgeInsets.only(left: 28.0, right: 28.0),
child: Image.asset("assets/prologo.png"),
),
Container(
margin: EdgeInsets.only(top: 10),
child: Center(
child: Text(
"Phone (OTP) Authentication",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
)),
),
SizedBox(
height: 50,
),
SizedBox(
height: 60,
width: 400,
child: CountryCodePicker(
onChanged: (country) {
setState(() {
dialCodeDigits = country.dialCode;
});
},
initialSelection: "NPL",
showCountryOnly: false,
showOnlyCountryWhenClosed: false,
favorite: ["+91", "IND", "+1", "USA"],
),
),
Container(
margin: EdgeInsets.only(top: 10, right: 10, left: 10),
child: TextField(
decoration: InputDecoration(
hintText: "Phone Number",
prefix: Padding(
padding: EdgeInsets.all(4),
child: Text(dialCodeDigits),
)),
maxLength: 12,
keyboardType: TextInputType.number,
controller: _controller,
)),
Container(
margin: EdgeInsets.all(15),
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
var numberPhone = dialCodeDigits + _controller.text;
FirebaseDatabase.instance
.reference()
.child('Subscribers')
.child('number')
.equalTo(numberPhone)
.once()
.then((DataSnapshot snapshot) {
Map numPhone = snapshot.value;
var phoneNumber = numPhone['number'];
print(phoneNumber);
});
if ('+917340868077' == numberPhone) {
Navigator.of(context).push(MaterialPageRoute(
builder: (c) => OTPController(
phoneNumber: _controller.text,
codeDigit: dialCodeDigits)));
}
},
child: Text(
'Next',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
)
],
),
),
);
}
}
【问题讨论】:
标签: android flutter dart firebase-realtime-database