Flutter社交媒体应用程序通知页面错误



我现在正在开发一个社交媒体应用程序,现在正在制作通知页面。当有人关注时发出警报很好,但当有人喜欢帖子时,你会在通知页面上看到红色的颤动错误。上面写着"package:cached_network_image/src/cached_network_iimage_provider.dart':断言失败:第18行位置16:'url!=null":不为真"但为什么呢?这里是页面的代码

import 'package:buddiesgram/pages/HomePage.dart';
import 'package:buddiesgram/pages/PostScreenPage.dart';
import 'package:buddiesgram/pages/ProfilePage.dart';
import 'package:buddiesgram/widgets/HeaderWidget.dart';
import 'package:buddiesgram/widgets/ProgressWidget.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:timeago/timeago.dart' as tAgo;
class NotificationsPage extends StatefulWidget {
@override
_NotificationsPageState createState() => _NotificationsPageState();
}
class _NotificationsPageState extends State<NotificationsPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: header(context, strTitle: 'Notifications',),
body: Container(
child: FutureBuilder(
future: retrieveNotifications(),
builder: (context, dataSnapshot)
{
if(!dataSnapshot.hasData)
{
return circularProgress();
}
return ListView(children: dataSnapshot.data,);
},
),
),
);
}
retrieveNotifications() async
{
QuerySnapshot querySnapshot = await activityFeedReference.document(currentUser.id)
.collection("feedItems").orderBy("timestamp", descending: true)
.limit(60).getDocuments();
List<NotificationsItem> notificationsItems = [];
querySnapshot.documents.forEach((document){
notificationsItems.add(NotificationsItem.fromDocument(document));
});
return notificationsItems;
}
}

String notificationItemText;
Widget mediaPreview;
class NotificationsItem extends StatelessWidget
{
final String username;
final String type;
final String commentData;
final String postId;
final String userId;
final String userProfileImg;
final String url;
final Timestamp timestamp;
NotificationsItem({this.username, this.type, this.commentData, this.postId, this.userId, this.userProfileImg, this.url, this.timestamp});
factory NotificationsItem.fromDocument(DocumentSnapshot documentSnapshot)
{
return NotificationsItem(
username: documentSnapshot["username"],
type: documentSnapshot["type"],
commentData: documentSnapshot["commentData"],
postId: documentSnapshot["postId"],
userId: documentSnapshot["userId"],
userProfileImg: documentSnapshot["userProfileImg"],
url: documentSnapshot["url"],
timestamp: documentSnapshot["timestamp"],
);
}
@override
Widget build(BuildContext context)
{
configureMediaPreview(context);

return Padding(
padding: EdgeInsets.only(bottom: 2.0),
child: Container(
color: Colors.white54,
child: ListTile(
title: GestureDetector(
onTap: ()=> displayUserProfile(context, userProfileId: userId),
child: RichText(
overflow: TextOverflow.ellipsis,
text: TextSpan(
style: TextStyle(fontSize: 14.0, color: Colors.black),
children: [
TextSpan(text: username, style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: " $notificationItemText"),
],
),
),
),
leading: CircleAvatar(
backgroundImage: CachedNetworkImageProvider(userProfileImg),
),
subtitle: Text(tAgo.format(timestamp.toDate()), overflow: TextOverflow.ellipsis,),
trailing: mediaPreview,
),
),
);
}
configureMediaPreview(context)
{
if(type == "comment" || type == "like")
{
mediaPreview = GestureDetector(
onTap: ()=> displayFullProfile(context, userProfileId: currentUser.id),
child: Container(
height: 50.0,
width: 50.0,
child: AspectRatio(
aspectRatio: 16/9,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(fit: BoxFit.cover, image: CachedNetworkImageProvider(url)),
),
),
),
),
);
}
else
{
mediaPreview = Text("");
}
if(type == "like")
{
notificationItemText = "liked your post.";
}
else if(type == "comment")
{
notificationItemText = "replied: $commentData";
}
else if(type == "follow")
{
notificationItemText = "started following you.";
}
else
{
notificationItemText = "Error, Unknown type = $type";
}
}
displayFullProfile(BuildContext context, {String userProfileId})
{
Navigator.push(context, MaterialPageRoute(builder: (context)=> PostScreenPage(postId: postId, userId: userId,)));
}
displayUserProfile(BuildContext context, {String userProfileId})
{
Navigator.push(context, MaterialPageRoute(builder: (context) => ProfilePage(userProfileId: userProfileId,)));
}
}

这个错误是在这个页面上还是在另一个页面上?

这是cached_network_image_provider.dart

import 'dart:async' show Future;
import 'dart:io' show File;
import 'dart:typed_data';
import 'dart:ui' as ui show instantiateImageCodec, Codec;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
typedef void ErrorListener();
class CachedNetworkImageProvider
extends ImageProvider<CachedNetworkImageProvider> {
/// Creates an ImageProvider which loads an image from the [url], using the [scale].
/// When the image fails to load [errorListener] is called.
const CachedNetworkImageProvider(this.url,
{this.scale: 1.0, this.errorListener, this.headers, this.cacheManager})
: assert(url != null),
assert(scale != null);
final BaseCacheManager cacheManager;
/// Web url of the image to load
final String url;
/// Scale of the image
final double scale;
/// Listener to be called when images fails to load.
final ErrorListener errorListener;
// Set headers for the image provider, for example for authentication
final Map<String, String> headers;
@override
Future<CachedNetworkImageProvider> obtainKey(
ImageConfiguration configuration) {
return SynchronousFuture<CachedNetworkImageProvider>(this);
}
@override
ImageStreamCompleter load(
CachedNetworkImageProvider key, DecoderCallback decode) {
return MultiFrameImageStreamCompleter(
codec: _loadAsync(key),
scale: key.scale,
informationCollector: () sync* {
yield DiagnosticsProperty<ImageProvider>(
'Image provider: $this n Image key: $key',
this,
style: DiagnosticsTreeStyle.errorProperty,
);
},
);
}
Future<ui.Codec> _loadAsync(CachedNetworkImageProvider key) async {
var mngr = cacheManager ?? DefaultCacheManager();
var file = await mngr.getSingleFile(url, headers: headers);
if (file == null) {
if (errorListener != null) errorListener();
return Future<ui.Codec>.error("Couldn't download or retrieve file.");
}
return await _loadAsyncFromFile(key, file);
}
Future<ui.Codec> _loadAsyncFromFile(
CachedNetworkImageProvider key, File file) async {
assert(key == this);
final Uint8List bytes = await file.readAsBytes();
if (bytes.lengthInBytes == 0) {
if (errorListener != null) errorListener();
throw Exception("File was empty");
}
return await ui.instantiateImageCodec(bytes);
}
@override
bool operator ==(dynamic other) {
if (other.runtimeType != runtimeType) return false;
final CachedNetworkImageProvider typedOther = other;
return url == typedOther.url && scale == typedOther.scale;
}
@override
int get hashCode => hashValues(url, scale);
@override
String toString() => '$runtimeType("$url", scale: $scale)';
}

有人能帮忙吗?

我发现,当我用CircleAvatar删除NotificationsPage中的Leading时,错误不会出现,这意味着CachedNetworkImageProvider就是错误制造者。但也许有人能告诉我为什么吗?

最新更新