【发布时间】:2021-01-08 08:12:41
【问题描述】:
我正在开发一款纸牌游戏,用户可以在其中添加新卡片。每张卡片都有一个图像路径。 我正在尝试使用来自另一个应用程序(例如:图库)的图像预填充我的新卡屏幕。 为此,我使用包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()。
【问题讨论】: