gpt4 book ai didi

firebase - 注销时未处理 AutoDispose StreamProvider

转载 作者:行者123 更新时间:2023-12-04 07:25:46 29 4
gpt4 key购买 nike

目前,我们正在使用 Firebase 在我们的应用程序上实现简单的聊天。

我们使用 Riverpod 处理应用程序的启动和身份验证。

启动过程如下:

@override
Widget build(BuildContext context) {
LocalNotificationService()
.handleApplicationWasLaunchedFromNotification(_onSelectNotification);
LocalNotificationService().setOnSelectNotification(_onSelectNotification);
_configureDidReceiveLocalNotification();

// final navigator = useProvider(navigatorProvider);
final Settings? appSettings = useProvider(settingsNotifierProvider);
final bool darkTheme = appSettings?.darkTheme ?? false;
final LauncherState launcherState = useProvider(launcherProvider);

SystemChrome.setEnabledSystemUIOverlays(
<SystemUiOverlay>[SystemUiOverlay.bottom],
);

return MaterialApp(
title: 'Thesis Cancer',
theme: darkTheme ? ThemeData.dark() : ThemeData.light(),
navigatorKey: _navigatorKey,
debugShowCheckedModeBanner: false,
home: Builder(
builder: (BuildContext context) => launcherState.when(
loading: () => SplashScreen(),
needsProfile: () => LoginScreen(),
profileLoaded: () => MainScreen(),
),
),
);
}

目前,我们只允许从主屏幕和房间屏幕退出,如下所示:

ListTile(
leading: const Icon(Icons.exit_to_app),
title: const Text('Çıkış yap'),
onTap: () =>
context.read(launcherProvider.notifier).signOut(),
),

signOut 的作用:

Future<void> signOut() async {
tokenController.state = '';
userController.state = User.empty;
await dataStore.removeUserProfile();
_auth.signOut();
state = const LauncherState.needsProfile();
}

问题是,每次我们进入 RoomsPage 并从它或主页面注销(从房间返回)时,我们都会遇到与 firebase< 相同的问题/强>: enter image description here调用者没有执行指定操作的权限。。当然,signout 会关闭 Firebase,因此 Firebase 会抛出此错误;但是,它应该在从 RoomsScreen 出来之后(即使返回主屏幕也会发生),这个小部件被处置因此连接应该被关闭,处置,但它似乎仍然是在内存上。

房间页面界面如下:

class RoomsPage extends HookWidget {
@override
Widget build(BuildContext context) {
final AsyncValue<List<fc_types.Room>> rooms =
useProvider(roomsListProvider);
return Scaffold(
appBar: Header(
pageTitle: "Uzmanlar",
leading: const BackButton(),
),
endDrawer: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 275),
child: SideMenu(),
),
body: rooms.when(
data: (List<fc_types.Room> rooms) {
if (rooms.isEmpty) {
return Container(
alignment: Alignment.center,
margin: const EdgeInsets.only(
bottom: 200,
),
child: const Text('No rooms'),
);
}

return ListView.builder(
itemCount: rooms.length,
itemBuilder: (
BuildContext context,
int index,
) {
final fc_types.Room room = rooms[index];

return GestureDetector(
onTap: () => pushToPage(
context,
ChatPage(
room: room,
),
),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: Row(
children: <Widget>[
Container(
height: 40,
margin: const EdgeInsets.only(
right: 16,
),
width: 40,
child: ClipRRect(
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
child: Image.network(room.imageUrl ?? ''),
),
),
Text(room.name ?? 'Room'),
],
),
),
);
},
);
},
loading: () => const Center(
child: CircularProgressIndicator(),
),
error: (Object error, StackTrace? stack) => ErrorScreen(
message: error.toString(),
actionLabel: 'Home',
onPressed: () => Navigator.of(context).pop(),
),
),
);
}
}

提供者很简单:

final AutoDisposeStreamProvider<List<fc_types.Room>> roomsListProvider =
StreamProvider.autoDispose<List<fc_types.Room>>(
(_) async* {
final Stream<List<fc_types.Room>> rooms = FirebaseChatCore.instance.rooms();
await for (final List<fc_types.Room> value in rooms) {
yield value;
}
},
name: "List Rooms Provider",
);

我想 AutoDispose 构造函数会在删除小部件时自动处理此提供程序,因此,它应该关闭与 Firebase 的连接(如文档所述)。

这里有什么问题?

我错过了什么?

我应该打开一个关于这个的问题吗?

最佳答案

documentation ,该示例使用基于 StreamController

Stream
final messageProvider = StreamProvider.autoDispose<String>((ref) async* {
// Open the connection
final channel = IOWebSocketChannel.connect('ws://echo.websocket.org');

// Close the connection when the stream is destroyed
ref.onDispose(() => channel.sink.close());

// Parse the value received and emit a Message instance
await for (final value in channel.stream) {
yield value.toString();
}
});

在您的例子中,您的方法返回一个 Stream。这改变了游戏规则。只需返回 Stream

final AutoDisposeStreamProvider<List<fc_types.Room>> roomsListProvider =
StreamProvider.autoDispose<List<fc_types.Room>>(
(_) => FirebaseChatCore.instance.rooms(),
name: "List Rooms Provider",
);

关于firebase - 注销时未处理 AutoDispose StreamProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68225873/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com