请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

HiCache在web环境中使用的问题

老师您好,6-9的登录DEMO在安卓模拟器中运行正常,但是在Chrome浏览器中运行时,页面加载后会报出以下异常,而且似乎无法从HiCache中取值(返回null),请问web应用使用本地存储需要额外操作,还是暂时不能使用HiCache操作本地存储?

以下是main方法

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Login Demo',
        theme: ThemeData(
          useMaterial3: true,
          primarySwatch: Colors.blue,
        ),
        home: FutureBuilder<dynamic>(
            future: HiCache.preInit(),
            builder: (context, AsyncSnapshot<dynamic> snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                //获取登录的token
                if (LoginDao.getFromSp() == null) {
                  return const LoginPage();
                } else {
                  return const HomePage();
                }
              } else {
                return const Scaffold(
                  body: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
              }
            }));
  }
}

这个是异常抛出的地方:
图片描述
以下是控制台输出:

Waiting for connection from debug service on Chrome...
This app is linked to the debug service: ws://127.0.0.1:55202/6JB9tWtoPmQ=/ws
Debug service listening on ws://127.0.0.1:55202/6JB9tWtoPmQ=/ws
Debug service listening on ws://127.0.0.1:55202/6JB9tWtoPmQ=/ws
Error
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 297:3                      throw_
packages/shared_preferences_platform_interface/shared_preferences_platform_interface.dart 100:5  getAllWithParameters
packages/shared_preferences_web/shared_preferences_web.dart 54:12                                getAll
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                               runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5                               _async
packages/shared_preferences_web/shared_preferences_web.dart 53:37                                getAll
packages/shared_preferences/shared_preferences.dart 255:38                                       _getSharedPreferencesMap
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                               runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5                               _async
packages/shared_preferences/shared_preferences.dart 229:62                                       _getSharedPreferencesMap
packages/shared_preferences/shared_preferences.dart 78:19                                        getInstance
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                               runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5                               _async
packages/shared_preferences/shared_preferences.dart 71:47                                        getInstance
packages/flutter_hi_cache/flutter_hi_cache.dart 30:39                                            init
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                               runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5                               _async
packages/flutter_hi_cache/flutter_hi_cache.dart 29:12                                            init
packages/flutter_hi_cache/flutter_hi_cache.dart 8:5                                              __
packages/flutter_hi_cache/flutter_hi_cache.dart 25:27                                            getInstance
packages/my_flutter_test/third/dao/login_dao.dart 44:34                                          getFromSp
packages/my_flutter_test/third/main.dart 29:42                                                   <fn>
packages/flutter/src/widgets/async.dart 609:48                                                   build
packages/flutter/src/widgets/framework.dart 5592:27                                              build
packages/flutter/src/widgets/framework.dart 5480:15                                              performRebuild
packages/flutter/src/widgets/framework.dart 5643:11                                              performRebuild
packages/flutter/src/widgets/framework.dart 5196:7                                               rebuild
packages/flutter/src/widgets/framework.dart 2904:18                                              buildScope
packages/flutter/src/widgets/binding.dart 989:9                                                  drawFrame
packages/flutter/src/rendering/binding.dart 448:5                                                [_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1386:7                                               [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1311:9                                               handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1034:7                                               <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 48:11                          internalCallback

正在回答 回答被采纳积分+3

1回答

CrazyCodeBoy 2024-06-30 13:39:12

看log是HiCache依赖的shared_preferences插件在浏览器上没有对应实现,你用的shared_preferences是哪个版本呢?将它升级到最新版本再试一试呢

0 回复 有任何疑惑可以回复我~
  • 提问者 苏狗牙 #1
    Flutter version 3.19.0 on channel stable 以及 shared_preferences 2.2.3
    回复 有任何疑惑可以回复我~ 2024-06-30 14:27:39
  • CrazyCodeBoy 回复 提问者 苏狗牙 #2
    我也用shared_preferences 2.2.3这个版本试试
    回复 有任何疑惑可以回复我~ 2024-06-30 14:33:53
  • 提问者 苏狗牙 回复 CrazyCodeBoy #3
    回复 CrazyCodeBoy:我知道为什么了,我不小心在shared_preferences_web.dart 里面方法名加了一个字母,把它去掉就可以了。没想到外部依赖的代码为啥还可以直接改呢?
    回复 有任何疑惑可以回复我~ 2024-06-30 15:21:35
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信