How to distinguish between types of darts

I have the following types:

class AddressEditor extends TextEditor {}
class TypeEditor extends TextEditor {}

      

I tried to identify the editors as such:

void validationErrorHandler( ValidationError e )
{
  var editor = e.editor;
  if( editor is AddressEditor )
   print( editor.runtimeType.toString() ) // prints TextEditor

  if( editor is TypeEditor )
   print( editor.runtimeType.toString() ) // prints TextEditor
}

      

If I use mirrors

import 'dart:mirrors'; 

getTypeName(dynamic obj) 
{
  return reflect(obj).type.reflectedType.toString();
}

void validationErrorHandler( ValidationError e )
{
  var editor = e.editor;
  if( editor is AddressEditor )
   print( getTypeName( editor ) ) // prints TextEditor

  if( editor is TypeEditor )
   print( getTypeName( editor ) ) // prints TextEditor
}

      

Why type of editor TypeEditor

and AddressEditor

can not be identified? Yes, I know it is either TextEditor

, but is there a way to identify TypeEditor

or AddressEditor

in Dart.

I need these IDs to work with the validation result.

thank

+3


source to share


1 answer


UPDATE

It turns out to TextEditor

have a method newInstance()

that gets called to get new instances of the editor using BWU Datagrid

(basically TextEditor

a factory and implementation in one).

Since TypeEditor

and AddressEditor

do not override this method, internal clean instances are created TextEditor

.

To get the desired behavior, you need to override newInstance

and implement the constructor used by this method. Since the constructor in TextEditor

is private, it cannot be reused and must be copied (I'll change my mind on this project). The first two lines of the copied constructor need some adaptation.

The AddressEditor will now look like

class AddressEditor extends TextEditor {
  AddressEditor() : super();

  @override
  TextEditor newInstance(EditorArgs args) {
    return new AddressEditor._(args);
  }

  AddressEditor._(EditorArgs args) {
    this.args = args;
    $input = new TextInputElement()
      ..classes.add('editor-text');
    args.container.append($input);
    $input
      ..onKeyDown.listen((KeyboardEvent e) {
      if (e.keyCode == KeyCode.LEFT || e.keyCode == KeyCode.RIGHT) {
        e.stopImmediatePropagation();
      }
    })
      ..focus()
      ..select();
  }
}

      



TypeEditor

is the same as another class and constructor name.

ORIGINAL

I'm sure the above example is

works fine and that the problem lies elsewhere (these values ​​are not AddressEditors

or TypeEditors

, but simply TextEditors

.

class TextEditor {}

class AddressEditor extends TextEditor {}
class TypeEditor extends TextEditor {}

void main() {
  check(new AddressEditor());
  check(new TypeEditor());
  check(new TextEditor());
}

void check(TextEditor editor) {
  if(editor is AddressEditor) print('AddressEditor: ${editor.runtimeType}');
  if(editor is TypeEditor) print('TypeEditor: ${editor.runtimeType}');
  if(editor is TextEditor) print('TextEditor: ${editor.runtimeType}');
}

      

output

AddressEditor: AddressEditor
TextEditor: AddressEditor

TypeEditor: TypeEditor
TextEditor: TypeEditor

TextEditor: TextEditor

      

+4


source







All Articles