我试着用Dart写不可变的代码。Dart并不是真正考虑到不可变的,这就是为什么我需要写很多样板才能实现不变性。正因为如此,我对像Scala这样的语言如何解决这个问题产生了兴趣,它是围绕着不可变的概念构建的。
我目前在Dart中使用以下类:
class Profile{
List<String> _inSyncBikeIds = []; // private field
String profileName; // public field
Profile(this.profileName); // You should not be able to pass a va
省道里有*?操作符吗?
我正在使用copy_with_extension生成代码,目前我的错误如下
[SEVERE] copy_with_extension_gen:copy_with_extension_gen on lib/models/my_model.dart:
An error `FormatterException` occurred while formatting the generated source for
`package:app/models/my_model.dart`
which was output to
`lib/models/my_model.cop
我怎样才能获得我的代码(使用冻结的颤动)来使用"copyWith“冻结特性来更新这里的状态? 冻结的类: import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'todo_list.freezed.dart';
part 'todo_list.g.dart';
@freezed
class TodoList with _$TodoList {
cons
我是新来的达特。这是我的密码:
class Point {
int x;
int y;
Point(this.x, this.y);
Point.same(int i) {
x = i;
y = i;
}
}
main() {}
我在运行它时得到了这个错误。
bin/dart1.dart:7:3: Error: This constructor should initialize field 'x' because its type 'int' doesn't allow null.
Point.same(i
我有一个带有几个参数的类,比如class Building(val a: Int, val b: Int, val c: Int)。我需要更新的代码是:
def updatedA(a: Int): Building = new Building(a, this.b, this.c)
def updatedB(b: Int): Building = new Building(this.a, b, this.c)
有没有一种更短的方法来获得一个更新的对象,如下所示?
def updatedA(newA: Int): Building = new { val a = newA } extends t
什么是省道颤振中的零参数构造函数?
在创建“颤振”框时,我收到以下错误
The superclass 'Bloc<QuoteEvent, QuoteState>' doesn't have a zero argument constructor.
Try declaring a zero argument constructor in 'Bloc<QuoteEvent, QuoteState>', or explicitly invoking a different constructor in 'Bloc<Qu
考虑以下职能:
BasicClass copyWith({
String id,
}) {
// some code behaving differently for 1) id is undefined and 2) id is explicit null
}
并考虑以下两个参数:
无(id未定义)
copyWith();
空(id为空)
copyWith(id: null);
在copyWith方法中,有什么方法可以使它对1)和2的行为有所不同吗?
Dart的插件在自动生成一些容易出错和繁琐的代码部分方面做了很大的工作,以换取一些样板:两种方法、一种注释和一种对生成文件的引用。
import 'package:json_annotation/json_annotation.dart';
part 'location.g.dart';
@JsonSerializable()
class Location {
final double lat;
final double lng;
Location(this.lat, this.lng);
factory Location.fromJso
当我试图理解Dart中的copyWith()方法在ThemeData类中的功能时,我创建了这个函数,但是当我使用它时,它并没有改变我之前传递的第一个值,这与我的预期相反,而且我也不知道我的代码中遗漏了什么导致了这个问题。
My函数:
enum Color {
RED,
BLUE,
GREEN,
YELLOW,
BLACK,
}
enum Brightness {
LIGHT,
DARK,
}
class ThemeData {
final Color accentColor;
final Color backgroundColor;
final Co
如何抽象方法具有可选参数?
abstract class CopyWith<T>{
T copyWith({}); // Error : Expected an identifier.
}
如果我添加一个类似于identifier的{test},它可以工作,子类可以有额外的参数。
我想实现什么?
我有一个复杂的状态管理器,我做了一些抽象,下面的代码是一个最小的代码,显示我的问题
import 'dart:collection';
abstract class CopyWith<T> {
T copyWith(OPTIONAL_NAMED_ARG
我遇到了一个问题。颤振调试器说,“空值上使用的null检查操作符”,突出显示了framework.dart中下面的代码。
/// The configuration for this element.
///
/// Avoid overriding this field on [Element] subtypes to provide a more
/// specific widget type (i.e. [StatelessElement] and [StatelessWidget]).
/// Instead, cast a
我使用github 在颤振中实现了PageView视差效应。在“零安全迁移”之后,我面临以下错误。
======== Exception caught by widgets library =======================================================
The following _CastError was thrown building PageTransformer(dirty, state: _PageTransformerState#a4851):
Null check operator used on a null value
我对飞
为什么在Dart的空安全之后List()构造函数不可访问?
// Compile time error: 'List' is deprecated and shouldn't be used.
// The default 'List' constructor isn't available when null safety is enabled.
// Try using a list literal, 'List.filled' or 'List.generate'.
List<int> foo
我试图为我的类创建一个"copyWith“方法,它适用于大多数场景。
问题是当我试图将一个可空属性设置为null时,因为我的函数无法识别它是否有意的。
前:
class Person {
final String? name;
Person(this.name);
Person copyWith({String? name}) => Person(name ?? this.name);
}
void main() {
final person = Person("Gustavo");
print(person.name); //
我声明了FirebaseFirestore和FireBaseAuth类型的两个变量,现在我面临一个空安全错误。我在类中使用一个构造函数,并将它们作为参数传递,得到的错误为- The parameter 'firebaseFirestore' can't have a value of 'null' because of its type, but the implicit default value is 'null' in flutter和The parameter 'firebaseAuth' can't ha
在生成时收到的错误:
NoSuchMethodError:方法'map‘被调用为null。接收方:空尝试调用:Map(闭包: Transaction=>容器)
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building TransactionList(dirty):
The method 'map' was called