我正在开发一个卡片游戏,用户可以在其中添加新卡片。每张卡片都有一个图像路径。我正在尝试使用来自另一个应用程序(例如:图库)的图像预填充我的新卡屏幕。为此,我正在使用包receive_sharing_intent,已成功设法打印以控制台我的应用程序中的图像路径。现在我不能做的是在获取图像后重定向到新屏幕。这是我的主屏幕上的相关代码:
class Menu extends StatefulWidget {
final String title;
Menu({Key key, this.title}) : super(key: key);
@override
_MenuState createState() => _MenuState();
}
class _MenuState extends State<Menu> {
StreamSubscription _intentDataStreamSubscription;
List<SharedMediaFile> _sharedFiles;
@override
void initState() {
super.initState();
bootstrapCards();
getOurCards();
// For sharing images coming from outside the app while the app is in the memory
_intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream()
.listen((List<SharedMediaFile> value) {
setState(() {
_sharedFiles = value;
print("Shared:" + (_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
_goCardAdd((_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
});
}, onError: (err) {
print("getIntentDataStream error: $err");
});
// For sharing images coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
setState(() {
_sharedFiles = value;
print("Shared:" + (_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
_goCardAdd((_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
});
});
}
@override
void dispose() {
_intentDataStreamSubscription.cancel();
super.dispose();
}
void _goCardAdd(path) {
MyCard myEmptyCard = new MyCard();
myEmptyCard.url=path;
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) => AddEditScreen(myCard: myEmptyCard)),
);
}
似乎 _goCardAdd 调用没有做任何事情,我不明白为什么。如果我把它放在 setState 调用之后,我会收到一个错误提示
在构建期间调用 setState() 或 markNeedsBuild() 。
似乎它可能是某种构建问题,因为我只对代码进行了微小的更改,它就开始工作了。同时我需要添加一个 if 子句来检查文件是否存在,因为如果没有,它总是会在我打开我的应用程序时将我重定向到新的卡片屏幕,即使没有图像文件正在进行中。检查下面的代码。
class _MenuState extends State<Menu> {
StreamSubscription _intentDataStreamSubscription;
List<SharedMediaFile> _sharedFiles;
@override
void initState() {
super.initState();
bootstrapCards();
// For sharing images coming from outside the app while the app is in the memory
_intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream()
.listen((List<SharedMediaFile> value) {
setState(() {
_sharedFiles = value;
print("Shared 1:" + (_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
if (_sharedFiles != null) _goCardAdd((_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
});
}, onError: (err) {
print("getIntentDataStream error: $err");
});
// For sharing images coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
setState(() {
_sharedFiles = value;
print("Shared 2:" + (_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
if (_sharedFiles != null) _goCardAdd((_sharedFiles?.map((f) => f.path)?.join(",") ?? ""));
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句