Cretezy / redux_persist

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

Store objects that contain objects list in them using Flutter #61

Closed Soykertje closed 4 years ago

Soykertje commented 4 years ago

This is no a issue, more like a question but i don't know another channel to do this question, so feel free to erase it if is necessary.

I'm new in Flutter and i don't know so much so please be patient.

Can i store objects that contain an object list inside of them using flutter?

Example:

class Purchases {
    List<Product> productsList = [];
    int purchaseId;
}

class Product {
    String name;
    double finalPrice;
    int unitsQuantity;
    double pricePerGram;
    double gramsQuantity;
}

There is any way to save it persistently with redux?

Cretezy commented 4 years ago

In your toJson method ( in Purchases), simply serialize the whole list (example):

toJson() => {
  "productsList": productsList.map((product) => product.toJson).toList(),
  "purchaseId": purchaseId,
}

static Purchases fromJson(data) => Purchases(
  productsList: (data["productsList"] as List)
                  .map((product) => Product.fromJson(product))
                  .toList(),
  purchaseId: data["purchaseId"],
);

And in product, simply implement the toJson/fromJson as per the examples in this repo.

Feel free to reopen if you have any additional questions!

Soykertje commented 4 years ago

Hi @Cretezy, when i was implementing redux (following the redux_persist_flutter_example.dart example), i got an issue that i don´t know why it happens.

The Intermediary class is for initializing and adding routes, the error appears at home.

Code:

class Intermediary extends StatelessWidget {
  final Store<Purchases> store;

  Intermediary({Key key, this.store}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return StoreProvider(
      store: store,
      child: MaterialApp(
        title: "",
        home: new MainScreenTwo("Cesta"),
      ),
    );
  }
}
class MainScreenTwo extends StatefulWidget {

  List<Product> arrProductsList = [];
  String appBarTitle;
  MainScreenTwo({
    this.appBarTitle;
  });

  @override
  _MainScreenTwoState createState() => _MainScreenTwoState(this.arrProductsList, this.appBarTitle);

}

Error message: The constructor returns type 'dynamic' that isn't of expected type 'Widget'

Personally i think it is because of StatefulWidget but i need it like that.

Cretezy commented 4 years ago

I'll need a fuller example. It seems like your doing something wrong, but that code doesn't help much. Can you make a single-file example to share?

Soykertje commented 4 years ago

Oh, sorry.

I was doing the example but i couldn't get the error again, i realized that i did a mistake, i have two MainScreen, one is for try changes and another like the stable version, when i copied the entire MainScreenTwo to MainScreen, i forgot to erase the Two one.

I almost finalice to implement Redux in my app but i have a question:

How can i save the objects with the type of data that i have? Something like call a method when i press a button.

Sorry for speding your time.