小编Dan*_*son的帖子

您可以为 Dart 中的函数添加属性吗?

您可以为 dart 中的函数添加属性吗?我试过这个:

void main(){

    fn(){
        //DoSomething
    };

    fn.id = 1; //Exception NoSuchMethod

}
Run Code Online (Sandbox Code Playgroud)

只是为了抛出 NoSuchMethod 异常。有没有什么方法可以像 JavaScript 一样在运行时向函数或任何类型的对象添加属性?

(另外,当我分配给属性时,为什么它说没有这样的方法异常?)

javascript methods properties dynamic dart

2
推荐指数
1
解决办法
1451
查看次数

dart 方法调用上下文

我使用下面的内容来查看 dart 如何调用传入其他方法的方法,以查看传入方法将/可以在什么上下文下调用。

void main() {

  var one = new IDable(1);
  var two = new IDable(2);

  print('one ${caller(one.getMyId)}');             //one 1
  print('two ${caller(two.getMyId)}');             //two 2
  print('one ${callerJustForThree(one.getMyId)}'); //NoSuchMethod Exception

}

class IDable{
  int id;
  IDable(this.id);

  int getMyId(){
    return id;
  }
}

caller(fn){
  return fn();
}

callerJustForThree(fn){
  var three = new IDable(3);
  three.fn();
}
Run Code Online (Sandbox Code Playgroud)

那么 manager 如何在没有上下文的情况下caller调用其参数ie ,以及为什么无法调用为其定义了该函数的对象传递的参数?fnone.fn()callerJustForThreefn

scope dart

2
推荐指数
1
解决办法
2337
查看次数

引用本地dart库

我正在写4个dart库A,B,C和D,它们都处于早期开发阶段,所以我不想在pub上发布它们.

还有一种情况是,只有A,B和C是公共图书馆,所有公共图书馆都依赖于D,这对于这三个图书馆来说应该是私有的.我如何管理这样的情况?

我可以使用pub在我的本地开发机器上为A,B和C安装库D,但它没有发布吗?如何在没有发布D的情况下完成A,B和C的发布,如果不被A,B或C使用,则D看起来不是特别有用?

我已经阅读了入门指南包结构文档,但似乎都没有涵盖这种情况,即如何管理私有库.还有这个问题,但用户在解决她的问题后没有回答.

dart dart-pub

2
推荐指数
1
解决办法
242
查看次数

css,如何阻止一个元素被容器压扁

提琴手

.outer当窗口被调整为比.outers 内容窄时,我想通过仅直接应用样式来阻止改变形状/大小,.outer如果可能的话。

HTML:

<div class='outer'>
    <div class='inner' style='background:#f00'>
    </div>
    <div class='inner' style='background:#0f0'>
    </div>
    <div class='inner' style='background:#00f'>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.outer, .inner{
    display: inline-block;
    position: relative;
    float: left;
    margin: 0;
    border: 0;
    padding: 0;
    overflow: visible;
}

.outer{
    /*what to put in here*/
}

.inner{
    width:50px;
    height:50px;
}
Run Code Online (Sandbox Code Playgroud)

在 jsfiddle 中,如果您将结果面板调整为更薄,您会看到蓝色被推到下一行,然后是绿色方块,我希望外部保持其原始形状/大小,以便三个.inners 保持它们的职位。

html css styles

2
推荐指数
1
解决办法
6042
查看次数

dart, a nicer way of mapping maps?

I like the .map((item){/*mapper*/}) that is on the Iterable classes, but the Map class doesn't appear to implement an analogous method. When I want to do the following, I have to create an blank instance and then forEach over the existing Map I want to map to the new instance type:

void noSuchMethod(Invocation inv){
  if(inv.isMethod){
    var namedArguments = new Map<String, dynamic>();
    inv.namedArguments.forEach((k, v){
      namedArguments[MirrorSystem.getName(k)] = v;
    });
    return;
  }
  super.noSuchMethod(inv);
}
Run Code Online (Sandbox Code Playgroud)

Is there a nicer way of mapping Maps? …

map dart

2
推荐指数
3
解决办法
2914
查看次数

dart,一种替换地图中键和值的好方法?

我有一个Map,我想通过valueskeys并用其他对象替换满足某些设定条件的特定对象的任何出现,因此如果我找到满足特定条件的键。
我想将该键交换为仍然指向映射中相同值对象的不同对象,类似地,如果我找到要替换的值,我希望原始键指向替换值。
这是一些适用于简化示例的代码,但它看起来很丑陋,有没有更好的方法来实现这一点,这意味着一种不需要您提取要替换的每个键和值然后将替换写回的方法在。
能够只迭代一次地图而不是迭代键,然后迭代所有要替换的键和值会更好吗?

void main(){
  //replace all values of instance A with an Object and all keys starting with "t" with the same key minus the "t"
  var m = {
    "one": new A(),
    "two": new A(),
    "three": new B(),
    "four": new B()
  };
  mapKeyAndValueSwapper(m,
    keyMeetsCondition: (k) => k.startsWith("t"),
    valueMeetsCondition: (v) => v is A,
    keyReplacer: (k) => k.replaceFirst("t", ""),
    valueReplacer: (v) => new Object());
  print(m);
}

mapKeyAndValueSwapper(Map m, {bool keyMeetsCondition(key), bool valueMeetsCondition(value), …
Run Code Online (Sandbox Code Playgroud)

map dart

2
推荐指数
1
解决办法
6314
查看次数

是否可以在不指定数据库的情况下连接到SQL Server?

我正在尝试为连接到SQL Server以实现持久性的代码编写一些单元测试,但我希望能够通过将其指向SQL Server实例来运行我的单元测试,并让测试创建自己的数据库运行测试,因此在每次测试之后,它可以在下一次测试重新创建之前删除数据库然后进行设置,因此我知道上一次测试中没有遗留的遗留数据或结构影响下一次测试.

c# sql sql-server unit-testing

2
推荐指数
1
解决办法
5768
查看次数

如何在Dart中实现可事件类型

是否有内置功能可以在dart中制作可事件类型?

在我的Javascript应用程序中,我使用名为Eventable的类来提供以下功能:

var dog = new Dog() //where Dog inherits from Eventable
var cat = new Cat() //where Cat inherits from Eventable

//use 'on' to listen to events
cat.on(dog, 'bark', cat.runaway); //assuming Cat has a method runaway on its prototype

//use fire to launch events
dog.fire({type: 'bark'});  //this causes cat.runaway(event); to be called
Run Code Online (Sandbox Code Playgroud)

在javascript中一个非常常见的模式,我喜欢它,因为它帮助我保持对象在src和我的脑海中孤立.

使用该on方法创建一个new EventContract具有基于所有者(cat上面),客户端(dog上面),类型('bark'上面)和功能(cat.runaway上面)的唯一密钥.这个唯一键允许我确保不EventContract创建重复的s,但更重要的是它允许我保持EventContract一个对象具有的所有s的易于查找的集合,这样我可以调用:

cat.dispose();
Run Code Online (Sandbox Code Playgroud)

并且所有的事件合同cat都将被销毁,所以我相信所有对cat的外部引用都已被删除,而cat现在可以被GC了.

但是我发现很难在Dart中实现这个功能,因为做类似的事情cat.on(dog, 'bark', cat.runaway); …

events design-patterns dart

1
推荐指数
1
解决办法
174
查看次数

访问dart中的继承字段

abstract class Painter {

    CanvasElement canvas;

    Painter(this.canvas);

    void draw();
}

class SpritePainter extends Painter{

    SpritePainter(this.canvas);

    void draw(){
        window.console.log("Drawing");
        window.console.log(canvas);
    }

}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码我的应用程序在尝试调用new SpritePainter(query('#sprite-canvas'));this.canvas是一个未知字段时失败.我认为CanvasElement抽象父类可以访问子类吗?

更新:

我修复了这个:

SpritePainter(CanvasElement canvas):super(canvas);
Run Code Online (Sandbox Code Playgroud)

但后来我读到飞镖教程,抽象类只能有工厂构造函数?

dart

1
推荐指数
1
解决办法
793
查看次数

dart导入和同一文件中的部分指令

我正在写一个飞镖文件:

import 'something.dart'

part of my_lib;

class A{
    //...
}
Run Code Online (Sandbox Code Playgroud)

我试过这个importpart of指令相反,它仍然无法工作,你有没有一个类文件作为库的一部分,并有进口?

import dart

1
推荐指数
1
解决办法
2089
查看次数

为什么GAE数据存储区不支持简单的struct字段类型?

我的单元测试失败并显示消息:

&errors.errorString {s:"datastore:unsupported struct field type:sus.Version"}

我有一个测试结构类型,我试图保存到GAE数据存储区:

type foo struct{
    sus.Version
}
Run Code Online (Sandbox Code Playgroud)

其中sus.Version是界面:

type Version interface{
    GetVersion() int
    getVersion() int
    incrementVersion() 
    decrementVersion() 
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用两个Version实现运行我的测试,首先它只是int的别名:

type version int
Run Code Online (Sandbox Code Playgroud)

其次作为结构:

type version struct{
    val int
}
Run Code Online (Sandbox Code Playgroud)

如果Version接口方法被赋予接收器类型(v *version),它需要是一个指针,因此递减和增量实际上更新它们被调用的版本,而不仅仅是一个副本.我不确定为什么这不起作用,可能是因为它是一个匿名字段?或者因为它是指向int或struct而不是实际的int或struct的指针?

go google-cloud-datastore

1
推荐指数
1
解决办法
1040
查看次数

javascript函数参数作为私有变量

我记得在某个地方读过函数参数变成函数内的私有变量所以我试过这个:

var node = function(nParent,nName,nContent){
    this.init = function(){
        alert(nName+" "+nParent+" "+nContent);
    }
};

var a = new node("A - parent","A - name","A - content");
var b = new node("B - parent","B - name","B - content");

a.init();
b.init();
Run Code Online (Sandbox Code Playgroud)

它提醒正确传入的参数,所以可以使用而不是这样的东西:

var node = function(nParent,nName,nContent){
    var parent = nParent;
    var name = nName;
    var content = nContent;
    this.init = function(){
        alert(name+" "+parent+" "+content);
    }
};
Run Code Online (Sandbox Code Playgroud)

我知道如果我想在将它们分配给私有变量之前对参数进行任何形式的额外验证检查,我将不得不使用第二个版本,如果参数已经是私有变量,我只是不想浪费空间永远不会去任何地方,这是一个合理的事情吗?谢谢,

javascript variables function

0
推荐指数
1
解决办法
202
查看次数

如何在golang中间接传递接口

我有一个方法包:

func Route(router *mux.Router){
    subrouter := router.PathPrefix(_API).Subrouter()
    subrouter.Path(_FOO).HandlerFunc(foo)
    subrouter.Path(_BAR).HandlerFunc(bar)
}
Run Code Online (Sandbox Code Playgroud)

我希望通过在我的包中使用匹配的接口来删除mux的外部依赖,这简单地包含了上面使用的所有功能,如下所示:

type Router interface{
    Path(string) Path
    PathPrefix(string) Path
}

type Path interface{
    HandlerFunc(http.HandlerFunc)
    Subrouter() Router
}

func Route(router Router){
    subrouter := router.PathPrefix(_API).Subrouter()
    subrouter.Path(_FOO).HandlerFunc(foo)
    subrouter.Path(_BAR).HandlerFunc(bar)
}
Run Code Online (Sandbox Code Playgroud)

但是当我构建这个时,我得到错误:

*mux.Router没有实现api.Router(Path方法的类型错误)有Path(string)*mux.Route想要Path(string)api.Path

但我认为接口是在golang中隐式使用的,所以我认为它*mux.Route确实实现了我的Path接口.

go

-1
推荐指数
1
解决办法
124
查看次数