Cretezy / redux_persist

Persist Redux State
https://pub.dartlang.org/packages/redux_persist
MIT License
130 stars 41 forks source link

Error compiling flutter build web #60

Closed nmacherey closed 4 years ago

nmacherey commented 4 years ago

Hi I am trying to compile a web version of my application and it seams to have some compilation errors comming from the package.

Could you help on this ?

  redux: ^4.0.0
  redux_epics: ^0.13.0
  redux_persist: ^0.8.3
  redux_persist_web: ^0.8.2 # web version
$ flutter clean
$ flutter build web

Target dart2js failed: Exception: ../../.pub-cache/hosted/pub.dartlang.org/redux_persist-0.8.3/lib/src/serialization.dart:6:22:
Error: Only external js-interop functions are supported.
Try removing 'external' keyword or annotating the function as a js-interop function.
  external Uint8List encode(T state);
                     ^
../../.pub-cache/hosted/pub.dartlang.org/redux_persist-0.8.3/lib/src/serialization.dart:8:14:
Error: Only external js-interop functions are supported.
Try removing 'external' keyword or annotating the function as a js-interop function.
  external T decode(Uint8List data);
             ^
../../.pub-cache/hosted/pub.dartlang.org/redux_persist-0.8.3/lib/src/storage.dart:8:25:
Error: Only external js-interop functions are supported.
Try removing 'external' keyword or annotating the function as a js-interop function.
  external Future<void> save(Uint8List data);
                        ^
../../.pub-cache/hosted/pub.dartlang.org/redux_persist-0.8.3/lib/src/storage.dart:11:30:
Error: Only external js-interop functions are supported.
Try removing 'external' keyword or annotating the function as a js-interop function.
  external Future<Uint8List> load();
                             ^
Error: Compilation failed.

initial-state.dart:

// ignore: uri_does_not_exist
import 'package:gb_drive/store/app/app.state.dart';

import 'initial-state-stub.dart'
  // ignore: uri_does_not_exist
  if (dart.library.html) 'initial-state-web.dart'
  // ignore: uri_does_not_exist
  if (dart.library.io) 'initial-state-io.dart';

Future<AppState> loadInitialState() => getInitialState();

Web Version:

import 'package:gb_drive/store/app/app.state.dart';
import 'package:redux_persist/redux_persist.dart';
import 'package:redux_persist_web/redux_persist_web.dart';

Persistor persistor;

Future<AppState> getInitialState() async {

  try {
    persistor = Persistor<AppState>(
      storage: WebStorage(),
      serializer: JsonSerializer<AppState>(AppState.fromJson), // Or use other serializers
    );

    return await persistor.load();
  } catch(e) {
    return null;
  }
}

IO Version

import 'dart:io';
import 'package:gb_drive/store/app/app.state.dart';
import 'package:path_provider/path_provider.dart';
import 'package:redux_persist/redux_persist.dart';

Persistor persistor;

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();

  return directory.path;
}

Future<File> get _localFile async {
  final path = await _localPath;
  return File('$path/state.json');
}

Future<AppState> getInitialState() async {
  try {
    persistor = Persistor<AppState>(
      storage: FileStorage(await _localFile),
      serializer: JsonSerializer<AppState>(AppState.fromJson), // Or use other serializers
    );

    return await persistor.load();
  } catch(e) {
    return null;
  }
}
Cretezy commented 4 years ago

Can you give me a small demo project I can play with? I haven't used the web library very much, and would like to make sure I can fix it properly for you

nmacherey commented 4 years ago

Hi Charles,

Thanks for your reply, I've tried removing the external declarations in the files and It successfuly compiles and work in a web environment.

I don't know why there should be these declarations. I will trye to set up a project for you

Thanx

ghost commented 4 years ago

@nmacherey what did you mean with this?

I've tried removing the external declarations in the files and It successfuly compiles and work in a web environment.

I'm experiencing the same issue, really don't know what to do.

I wonder if the import part is correct

import 'package:redux_persist/redux_persist.dart';
import 'package:redux_persist_flutter/redux_persist_flutter.dart' if (dart.library.io) 'package:redux_persist_flutter/redux_persist_flutter.dart';
import 'package:redux_persist_web/redux_persist_web.dart' if (dart.library.html) 'package:redux_persist_web/redux_persist_web.dart';
import 'package:flutter/foundation.dart' show kIsWeb;

class AtStore {
  static Future<Store<AppState>> createStore() async {

      final persistor = Persistor<AppState>(
        storage: kIsWeb == true ? WebStorage() : FlutterStorage(location: FlutterSaveLocation.documentFile), // Or use other engines
        serializer: JsonSerializer<AppState>(AppState.fromJson), // Or use other serializers
      );
      AppState initialState;
       initialState = await persistor.load(); 
      return Store(
        appReducer,
        initialState: initialState,
        middleware: [
          persistor.createMiddleware()
        ],
      );
  }
}
  flutter_redux: ^0.6.0
  redux_persist: ^0.8.2
  redux_persist_flutter: ^0.8.2  
  redux_persist_web: ^0.8.2
Cretezy commented 4 years ago

I have released redux_persist 0.8.4 with a fix for this. Thanks for the report!

realcarlos commented 4 years ago

Hi @Cretezy could you please upgrade the redux_persist_web ?

Cretezy commented 4 years ago

No need for redux_persist_web for Flutter, use redux_persist_flutter (see README for instructions)