flutter - NoSuchMethodError: ''方法未找到
问题描述
我正在尝试在 Flutter 上构建一个 Web 应用程序。我试图为我在网上找到的一些按钮添加一些代码,但我收到了这个错误,我不知道为什么。代码本身似乎没有错误,但是当我运行程序时出现此错误。
NoSuchMethodError: '<Unexpected Null Value>' method not found
该程序基本上看起来像一个应用栏:它显示了一堆矩形按钮,包括设置、注销等。在此之前,我成功地构建了背景和一个用于放置按钮的矩形。按钮本身会导致此错误。
任何帮助将非常感激!
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import '../time/time_view.dart';
import 'package:provider/provider.dart';
import '../../../app/constants/strings.dart';
import '../../../app/services/firebase_auth_service.dart';
import 'package:flutter_web_firebase_googe_auth/size_config.dart';
String name = '';
final FirebaseAuth _auth = FirebaseAuth.instance;
getCurrentUserName() async {
final user = await _auth.currentUser();
if (user.isAnonymous) {
name = 'Guest';
} else {
name = user.displayName;
}
}
class HomeView extends StatelessWidget {
const HomeView({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
// variable size
Size size = MediaQuery.of(context).size;
getCurrentUserName();
return Scaffold(
backgroundColor: Colors.blue[200],
body: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0)),
elevation: 5.0,
child: Container(
width: size.width,
height: size.height * .9,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: Text(
'\t\tHome Page \n Welcome',
style: Theme.of(context).textTheme.headline4,
),
),
const Spacer(),
SpecialOffers(),
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => TimeView()),
);
},
child: const Text(Strings.time),
),
const Spacer(),
RaisedButton(
onPressed: () {
context.read<FirebaseAuthService>().signOut();
},
child: const Text(Strings.signOut),
),
const Spacer(),
],
),
),
),
),
],
),
),
);
}
}
class SectionTitle extends StatelessWidget {
const SectionTitle({
Key key,
@required this.title,
@required this.press,
}) : super(key: key);
final String title;
final GestureTapCallback press;
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: TextStyle(
fontSize: getProportionateScreenWidth(18),
color: Colors.black,
),
),
GestureDetector(
onTap: press,
child: Text(
"See More",
style: TextStyle(color: Color(0xFFBBBBBB)),
),
),
],
);
}
}
class SpecialOffers extends StatelessWidget {
const SpecialOffers({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
Padding(
padding:
EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)),
child: SectionTitle(
title: "Special for you",
press: () {},
),
),
SizedBox(height: getProportionateScreenWidth(20)),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
SpecialOfferCard(
image: "assets/images/Image Banner 2.png",
category: "Smartphone",
numOfBrands: 18,
press: () {},
),
SpecialOfferCard(
image: "assets/images/Image Banner 3.png",
category: "Fashion",
numOfBrands: 24,
press: () {},
),
SizedBox(width: getProportionateScreenWidth(20)),
],
),
),
],
);
}
}
class SpecialOfferCard extends StatelessWidget {
const SpecialOfferCard({
Key key,
@required this.category,
@required this.image,
@required this.numOfBrands,
@required this.press,
}) : super(key: key);
final String category, image;
final int numOfBrands;
final GestureTapCallback press;
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(left: getProportionateScreenWidth(20)),
child: GestureDetector(
onTap: press,
child: SizedBox(
width: getProportionateScreenWidth(242),
height: getProportionateScreenWidth(100),
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Stack(
children: [
Image.asset(
image,
fit: BoxFit.cover,
),
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xFF343434).withOpacity(0.4),
Color(0xFF343434).withOpacity(0.15),
],
),
),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(15.0),
vertical: getProportionateScreenWidth(10),
),
child: Text.rich(
TextSpan(
style: TextStyle(color: Colors.white),
children: [
TextSpan(
text: "$category\n",
style: TextStyle(
fontSize: getProportionateScreenWidth(18),
fontWeight: FontWeight.bold,
),
),
TextSpan(text: "$numOfBrands Brands")
],
),
),
),
],
),
),
),
),
);
}
}
解决方案
NoSuchMethodError
通常应该指出哪条线导致了问题。这应该在完整的日志集中的某个地方找到。
从代码中,导致此错误的可能原因之一是FirebaseAuth _auth
. 您可能还想检查 FirebaseAuth 在您的 Provider 上是否为空context.read<FirebaseAuthService>().signOut();
。
推荐阅读
- node.js - 通过 post Handler 发送数据导致“无法设置标题”
- javascript - 如何根据页面 url react js 更改背景?
- django - Django 媒体 url 与 digitalocean 空间不匹配
- visual-studio-code - Julia:将代码发送到顶部的 Vscode 扩展?
- azure-keyvault - ARM KeyVault 访问策略有条件添加
- javascript - React-native:在渲染中显示文本(返回)有条件
- c# - Graphics.DrawImage 可以绘制 YV12 DIB 吗?
- reactjs - 反应设置间隔以更改报价
- swift - 在使用 CoreData 缓存 postItems 时,我在保存后看不到任何帖子
- .net - .NET Framework 与 .NET Standard 之间的区别?