2 years ago

#55097

test-img

Hardik

How to Initialize Multiple Objects in Flutter VxStore (VelocityX)

Please Check the MUTATION section where I've commented what is the issue:

store.dart

import 'package:velocity_x/velocity_x.dart';
import '../model.dart';

class MyStore extends VxStore {
  MyItem? itemA;
  MyItem? itemB;

  MyStore() {
    itemA = MyItem(); // note: 'new' keyword is obsolated now.
    itemB = MyItem();
  }
}

///----------- MUTATION-------------------///
class PrintMutation extends VxMutation<MyStore> {
  @override
  void perform() {
    print("...............Mutation Starts..................");
    store!.itemA!.name = "phone"; // assume we wrote some data into itemA
    store!.itemB = store!.itemA; // copying the data to itemB
    print('Item B:${store!.itemB!.name!}'); // prints "phone" (as needed)
    store!.itemB!.name = 'fruit'; // modifing the name
    print('Item A:${store!.itemA!.name!}'); // Item A changes to Fruit, WHY??
  }
}

main.dart

import 'package:flutter/material.dart';
import 'package:velocity_x/velocity_x.dart';
import 'core/store.dart';

void main() {
  runApp(VxState(child: MyApp(), store: MyStore()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () => PrintMutation(),
          child: "Print".text.make(),
        ),
      ),
    );
  }
}

pubspec.yaml

dependencies:
  velocity_x: ^3.3.0

flutter

class

dart

bloc

state-management

0 Answers

Your Answer

Accepted video resources