Giter Site home page Giter Site logo

Comments (6)

atreeon avatar atreeon commented on July 19, 2024 28

This would work but maybe there is a more succinct and descriptive method.

void main() {
  var bob2 = new Person2(age: 15, name: 'Bob');
  print(bob2.toString());

  var joe2 = bob2.copyWith(name: new Nullable<String>(null));
  print(joe2.toString());
}

class Person2 {
  final int age;
  final String name;

  Person2({this.age, this.name});

  Person2 copyWith({Nullable<int> age, Nullable<String> name}) {
    return new Person2(
      age: age == null ? this.age : age.value,
      name: name == null ? this.name : name.value,
    );
  }

  @override
  String toString() {
    return 'age: $age, name: $name';
  }
}

class Nullable<T> {
  T _value;

  Nullable(this._value);

  T get value {
    return _value;
  }
}

from flutter_redux.

brianegan avatar brianegan commented on July 19, 2024 2

Yah, I really hope they even get "data classes" like Kotlin, which would make this type-safe and easy. Please let me know if I can help further!

from flutter_redux.

atreeon avatar atreeon commented on July 19, 2024 1

That works a little nicer than my Nullable. I do love the javascript spread operator for this kind of thing though...I wonder if there was a way the Dart team could implement something like this (I'm guessing no due to the typed quality of the lanaguage...it does make my code nice and concise...I still prefer dart to js though!)

from flutter_redux.

brianegan avatar brianegan commented on July 19, 2024

Hey there -- yep, in this case, you'd need an extra class like the one you've proposed. You can either create a class just as you've done, or consider using the Optional class from the quiver library: https://pub.dartlang.org/packages/quiver

from flutter_redux.

pumuckelo avatar pumuckelo commented on July 19, 2024

Hi @brianegan , I just stumbled on the same problem and randomly found the solution to why my state isn't updating by randomly reading the copyWith Function.

What do you think of if we add a warning or information to the example of flutter_redux where the copyWith Method is shown? It's easier for other people then

from flutter_redux.

lukas-pierce avatar lukas-pierce commented on July 19, 2024

class Nullable {
T _value;

Nullable(this._value);

T get value {
return _value;
}
}

class Nullable<T> {
  final T? _value;

  const Nullable(this._value);

  T? get value => _value;
}

from flutter_redux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.