【发布时间】:2020-06-05 01:28:15
【问题描述】:
我正在尝试通过按钮在函数内调用 Firestore 数据,即集合的标题。当我只是在 onPressed 中使用它时它起作用了:
Expanded(
child: IconButton(
icon: Icon(
Icons.share,
onPressed: () => Share.share(
'${(snapshot.data.documents[index]['title'])}', //this displays the 'title'
fields of my firestore collections in ListView
))),
但是当试图从一个函数(在不同的共享插件中)调用时,无论我尝试什么都只是打印实际代码而不是内容,我如何在下面的函数中检索 firestore 集合的“标题”,我想字符串插值 ${ like this } 是唯一的方法,但它不适用于此:
MaterialButton(
child: Text('Share text'),
onPressed: () async => await _shareText(),
),
功能:
Future<void> _shareText() async {
try {
Share.text('my text title',
'HERE => This is my text to share with other applications.', 'text/plain');
// no variation of ${snapshot.data.documents[index]['title']} works here
} catch (e) {
print('error: $e');
}}
我得到数据的字符串是:
${snapshot.data.documents[index]['title']}
还有其他方法可以调用它吗?
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'dart:ui';
import 'package:google_fonts/google_fonts.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'dart:async';
import 'image.dart';
class Recipe extends StatefulWidget {
@override
_RecipeState createState() => _RecipeState();
}
class _RecipeState extends State<Recipe> {
var firestoreDb = Firestore.instance.collection("recipes").snapshots();
// THIS ${(snapshot.data.documents[index].data['title'])} does not work in
share plugin:
Future<void> _shareText() async {
try {
Share.text('Share Text ',
'${(snapshot.data.documents[index].data['title'])}', 'text/plain');
} catch (e) {
print('error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
padding: const EdgeInsets.all(8.0),
child: Text(
'Recipes',
style: GoogleFonts.lato(
fontSize: 22.0,
color: Colors.amberAccent.shade50,
),
)),
IconButton(
icon: Icon(MdiIcons.foodForkDrink, color: Color(0xffffe0b2),
size: 32.0),
onPressed: () {
null;
},
),
]),
backgroundColor: Colors.lightBlue,
elevation: 50.0,
), //AppBar
body: Container(
width: MediaQuery.of(context).size.width,
child: StreamBuilder(
stream: firestoreDb,
builder: (context, snapshot) {
if (!snapshot.hasData) Text('Loading...');
return StaggeredGridView.countBuilder(
crossAxisCount: 2,
mainAxisSpacing: 1.0,
crossAxisSpacing: 1.0,
padding: EdgeInsets.symmetric(horizontal: 2.0, vertical: 6.0),
shrinkWrap: true,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, int index) => Container(
child: Column(
children: <Widget>[
Card(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
child: Column(children: <Widget>[
ListTile(
title: Text(
'${(snapshot.data.documents[index]['title'])}',
style: GoogleFonts.lato(
fontSize: 20.0,
height: 1.2,
fontWeight: FontWeight.w500,
),
textAlign: TextAlign.center,
),
subtitle: Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
MaterialButton(
child: Text('Share text'),
onPressed: () async => await _shareText(),
),
SizedBox(height: 5.0),
ImageInput(), //this displays image called
from image_picker to share , it works for sharing an image.
Expanded(
child: Text(
'${(snapshot.data.documents[index]
['subtitle'])}',
textAlign: TextAlign.right,
style: GoogleFonts.lato(
fontSize: 13.0,
fontWeight: FontWeight.w900,
fontStyle: FontStyle.italic,
color: Colors.blue,
),
), //subtitle: Text
),
] //children
), //Row
), //listtile
]),
),
],
),
),
staggeredTileBuilder: (int index) => StaggeredTile.fit(2),
);
}),
),
);
} //build
} //class
我将 ImageInput() 类页面复制到主页中,因为我无法同时共享图像和文本,我仍然在共享文本字段中看到文本和快照的“未定义”错误。尽管它现在在同一页面上,但同样的修复不适用于文本。我究竟做错了什么?
MaterialButton(
child: Text('Send it!'),
onPressed: () async => await _shareImageAndText(snapshot, index),
),
---
Future<void> _shareImageAndText(snapshot, index) async {
try {
List<int> imageBytes = await _imageFile.readAsBytes();
var uint8List = Uint8List.fromList(imageBytes);
await Share.file('esys image', 'esys.jpg', uint8List, 'image/jpeg',
text: (snapshot.data.documents[index].data['title']) );
//text: 'My optional text.');
// (snapshot.data.documents[index].data['title']), 'text/plain');
} catch (e) {
print('error: $e');
}}
class ImageInput extends StatefulWidget {
@override
_ImageInputState createState() => _ImageInputState();
}
class _ImageInputState extends State<ImageInput> {
File _imageFile;
void _getImage(BuildContext context, ImageSource source){
ImagePicker.pickImage(source: source, maxWidth: 200.0).then((File image){
setState((){
_imageFile = image;
});
Navigator.pop(context);
});
}
void _openImagePicker(BuildContext context) {
showModalBottomSheet(context: context, builder: (BuildContext context) {
return Container(
height: 180.0,
padding: EdgeInsets.all(10.0),
child: Column(
children: [
Text('Choose photo',
style:TextStyle(fontWeight: FontWeight.bold),),
SizedBox(height: 10.0),
FlatButton(
textColor: Theme.of(context).primaryColor,
child: Text('Use Camera'),
onPressed: () {
_getImage(context, ImageSource.camera);
},),
FlatButton(
textColor: Theme.of(context).primaryColor,child:
Text('Open Gallery'),
onPressed: () {
_getImage(context, ImageSource.gallery);
},)
]
),);
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
OutlineButton(
onPressed: () {
_openImagePicker(context);
},
child:Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Icon(Icons.camera_alt),
SizedBox(
width:1.0,
),
// Text('Add Image'),
]
),//Row
),//outline button
SizedBox(height: 10.0),
_imageFile == null ? Text('Add a Pic') : Image.file(_imageFile,
fit: BoxFit.cover,
height: 200.0,
width: 200.0,
// width: MediaQuery.of(context).size.width,
alignment: Alignment.topCenter,
),
MaterialButton(
child: Text('Send it!'),
onPressed: () async => await _shareImageAndText(),
),
],
);
}
}
【问题讨论】:
-
但是函数里面的代码和你直接使用的代码是不一样的。比如Share.share()和Share.text()
-
没错,我从使用 Share.share 插件切换到 Share.text 插件,但 firestore 数据库字符串不适用于新包。它不显示内容,只有代码,我不明白为什么。
-
你能把你插件的链接发短信给我吗
-
@Henok link
-
该插件已经 2 年没有更新了,我建议将其更改为更活跃的库,如果您没有在字符串中获取标题,请尝试在调用分享并放入另一个变量中,String title = snapshot.data.documents[index]['title'],然后使用'some string value $title'之类的标题,让我知道结果。
标签: listview flutter dart google-cloud-firestore string-interpolation