类型"Null"不是类型"列表"的子类型<Name>



这是我改编为最近颤振版本的"网络忍者"代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:coffee_for_all/models/coffee.dart';
import 'package:coffee_for_all/services/auth.dart';
import 'package:flutter/material.dart';
import 'package:coffee_for_all/services/database.dart';
import 'package:provider/provider.dart';
import 'coffee_list.dart';
class Home extends StatelessWidget {
final AuthService _auth = AuthService();
var initialData;
@override
Widget build(BuildContext context) {
return StreamProvider<List<Coffee>>.value(
value: DatabaseService().coffees,
initialData: initialData,
child: Scaffold(
backgroundColor: Colors.brown[50],
appBar: AppBar(
title: Text('Coffee for all'),
backgroundColor: Colors.brown[400],
elevation: 0.0,
actions: <Widget>[
TextButton.icon(
icon: Icon(Icons.person),
label: Text('logout'),
style: TextButton.styleFrom(
primary: Colors.black,
),
onPressed: () async {
await _auth.signOut();
},
)
],
),
body: CoffeeList(),
),
);
}
}

当我尝试运行该程序时,会发生这种情况: ═════════════════════════════════════════════════════════════ 在 937 毫秒内重新加载了 680 个库中的 5 个。

════════控件库捕获的异常 ══════════════════ 类型"Null"不是类型"列表"的子类型 导致错误的相关小部件是 家 lib\screens\wrapper.dart:18

════════════════════════════════════════════════════════════════这就是我定义列表的位置和方式:

class Coffee {
final String? name;
final String? sugars;
final int? strength;
Coffee({this.name, this.sugars, this.strength});
}

class CoffeeList extends StatefulWidget {
@override
_CoffeeListState createState() => _CoffeeListState();
}
class _CoffeeListState extends State<CoffeeList> {
@override
Widget build(BuildContext context) {
final coffees = Provider.of<List<Coffee>>(context);
return ListView.builder(
itemCount: coffees.length,
itemBuilder: (context, index) {
return CoffeeTile(coffee: coffees[index]);
},
);
}
}

// coffee list from snapshot
List<Coffee> _coffeeListFromSnapShop(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
return Coffee(
name: doc.get('name') ?? '',
strength: doc.get('strength') ?? 0,
sugars: doc.get('sugars') ?? '0',
);
}).toList();
}
// get coffees stream
Stream<List<Coffee>> get coffees {
return coffeeCollection.snapshots().map(_coffeeListFromSnapShop);
}

任何人都可以帮助我解决此错误吗?谢谢!

试试这个解决方案。ListView.builder() 的 itemCount 属性没有空检查。

@override
Widget build(BuildContext context) {
final coffees = Provider.of<List<Coffee>>(context);
return ListView.builder(
itemCount: coffees!=null?coffees.length:0,
itemBuilder: (context, index) {
return CoffeeTile(coffee: coffees[index]);
},
);

} }

但这会导致:

════════ Exception caught by widgets library ═══════════════════════════════════
type 'Null' is not a subtype of type 'List<Coffee>'
The relevant error-causing widget was
Home
libscreenswrapper.dart:18
════════════════════════════════════════════════════════════════════════════════

作为我的包装器文件,这个:

import 'package:coffee_for_all/screens/authenticate/authenticate.dart';
import 'package:coffee_for_all/screens/home/home.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Wrapper extends StatelessWidget {
const Wrapper({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final user = Provider.of<Uid?>(context);
// return either Home or Authenticate widget
if (user == null) {
return Authenticate();
} else {
return Home();
}
}
}

问题是没有定义 initialData,但现在正在使用:

initialData: [],

相关内容

  • 没有找到相关文章

最新更新