每当我使用 flutter_bloc 关闭应用程序时如何重置为初始状态/值

Kar*_*hai 5 flutter

在使用方面我还是一个初学者flutter_bloc

我尝试flutter_bloc并好奇如何在关闭页面时将块类重置为其初始值。

my_bloc_class.dart

class sumtotal_detail_transactionsbloc extends Bloc<String, String>{
    @override
    String get initialState => '0';

    @override
    Stream<String> mapEventToState(String sumtotal_detail_transactions) async* {
        yield sumtotal_detail_transactions.toString();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的小部件带有BlocBuilder.

BlocBuilder<sumtotal_detail_transactionsbloc, String>(
    builder: (context,sumtotal_detail_transactions) => Text(
        sumtotal_detail_transactions,style: TextStyle(
                fontSize: 12,
                color: Colors.brown[300]
            ),
        )
),
Run Code Online (Sandbox Code Playgroud)

每当我关闭页面或导航到该页面时,如何始终/自动将sumtotal_detail_transactions其重置为初始值?

如果该值始终保持/存储原样,它将破坏我的应用程序。

Fel*_*lov 5

嘿,我建议在页面中提供块,这样当页面关闭时,BlocProvider 会自动处理块。不需要重置事件,只需确保将块范围仅限制到需要它的小部件树的部分即可。希望有帮助!