Flutter应用程序-在StreamBuilder中刷新元素时遇到麻烦



我有一个扑动应用程序,主要是上传和检索图像和从Firebase。我有一个特别的屏幕,用户可以在那里看到他们上传的记忆。

我的目标是让用户在图库中查看他们所有的记忆,并能够在同一屏幕上上传记忆。我已经成功地做到了这一点,但只是在需要刷新整个屏幕才能显示新图像的情况下。我想有我的建设者的屏幕只需要加载在newley上传的图像。为了实现这一点,我已经从FutureBuilder切换到StreamBuilder,但是新上传的图像仍然需要全屏重新加载。

下面是我的代码:


class _BrowseMemoriesScreenState extends State<BrowseMemoriesScreen> {
var index = 0;
var heroTag = "photo";
late Widget currentPage;
int dummy = 0;
late Stream<List<String>> streamData;
@override
void initState() {
super.initState();
streamData = BrowseMemoriesScreen(childID: widget.childID, childUID: widget.childUID, childKey: widget.childKey, childName: widget.childName, profilePicture: widget.profilePicture,)
.getMemories();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
final themeNotifier = Provider.of<ThemeNotifier>(context);
final isDarkMode = themeNotifier.theme == themeNotifier.darkTheme;
StreamController streamController = StreamController();
String imageURL = "";
String download;
return Scaffold(
appBar: SettingsAppBar(leadingIcon: true, title: Text("Memories")),
backgroundColor: isDarkMode ? Color.fromARGB(255, 47, 47, 47) : Colors.white,
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Child Icon
Center(
child: ChildButton(
key: widget.childKey,
childName: widget.childName,
childID: widget.childID,
profilePicture: widget.profilePicture,
childUID: widget.childUID,
editable: true,
),
),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
decoration: BoxDecoration(
color: isDarkMode ? Color.fromARGB(255, 47, 47, 47) : Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: StreamBuilder(
stream: streamData,
initialData: const [""],
builder: (context, AsyncSnapshot<List<String>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data!.isEmpty) {
return Center(
child: Text(
"No memories found yet. Uploaded Memories will appear here",
textAlign: TextAlign.center,
style: GoogleFonts.montserrat(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
);
}
try {
return GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 5,
mainAxisSpacing: 5,
),
itemCount: snapshot.data?.length,
itemBuilder: (context, index) {
var imgURL = snapshot.data?[index];
return GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => InspectPhoto(
snapshot: snapshot,
index: index,
heroTag: heroTag +
index.toString())));
},
child: Hero(
tag: heroTag + index.toString(),
child: GestureDetector(
onLongPress: () async {
ScreenshotController screenshotController = ScreenshotController();

await screenshotController.captureFromWidget(
CachedNetworkImage(
imageUrl: imgURL!,
// Placeholder appears as a loading indicator
//placeholder: (context, url) => Container(
// color: Colors.grey,
//),
errorWidget: (context, url, error) => Container(
child: Center(child: Text("Error loading requested image.")),
),
)
).then((Uint8List capturedImage) async {
final appDir = await getApplicationDocumentsDirectory();
File file = await File('${appDir.path}/sharedMemory.png').create();
await file.writeAsBytes(capturedImage);
await Share.shareXFiles(
[XFile(file.path)],
text: "Choose Where to Send Memory",
);
file.delete();
});
}, child:CachedNetworkImage(
imageUrl: imgURL!,
fit: BoxFit.cover,
),
)
),
);
});
} catch (e) {
return Center(
child: Text(
"No memories found. Uploaded Memories will appear here."));
}
} else if (snapshot.connectionState ==
ConnectionState.none) {
return Center(
child: Text(
"No network connection, try again later."));
}
return SizedBox(
height: 200,
width: 200,
child: Center(
child: CircularProgressIndicator(strokeWidth: 10),
),
);
}
/*child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
itemBuilder: (context, index) {
return RawMaterialButton(
onPressed: () {},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
image: DecorationImage(
image: AssetImage(_images[index]),
fit: BoxFit.cover,
),
),
),
);
},
itemCount: _images.length,
*/
),
),
),
],
),
),
);
}
}

/ Grab all of the memories that are associated with the childID
Stream<List<String>> getMemories() async* {
List<String> URLS = [];
DocumentSnapshot doc = await FirebaseMethods(childID: childID)
.getChildDoc(childUID);
var data = doc.data() as Map<String, dynamic>;
// These two lists will hold all of the child's memories - name of the file
// in Firebase plus the corresponding date that photo was created
List<String> memoryNames = [];
List<String> creationDates = [];
for (String memory in data['memories']['memoryNames']) {
memoryNames.add(memory);
}
for (String creationDate in data['memories']['creationDates']) {
creationDates.add(creationDate);
}
for (String memory in memoryNames) {
var URLRef = await firebase_storage.FirebaseStorage.instance
.ref()
.child('files/memories/')
.child('${memory}');
var imgURL = await URLRef.getDownloadURL();
URLS.add(imgURL);
}
yield URLS;
}

修复此错误。

没有更新我的函数,它会更新流数据,因为它会返回一个未来。还需要改变我访问数据的方式以及之后。

Stream<DocumentSnapshot> getChildDocStream(childUID) async* {
yield* FirebaseFirestore.instance
.collection('child')
.doc(childUID)
.snapshots();
}```

相关内容