Home > Back-end >  Flutter : create new instance of a class which no have default constructor and change some propertie
Flutter : create new instance of a class which no have default constructor and change some propertie

Time:10-02

I have a class with name "RecognisedText" in one of my packages which is required in my Flutter application. This class is:

class RecognisedText {
  RecognisedText._(this.text, this.blocks);

  factory RecognisedText.fromMap(Map<dynamic, dynamic> map) {
    var resText = map["text"];
    var textBlocks = <TextBlock>[];
    for (var block in map["blocks"]) {
      var textBlock = TextBlock.fromMap(block);
      textBlocks.add(textBlock);
    }
    return RecognisedText._(resText, textBlocks);
  }

  ///String containing all the text identified in a image.
  final String text;

  ///All the blocks of text present in image.
  final List<TextBlock> blocks;
}

I want to define a variable from this class:

RecognisedText myobject = RecognisedText(mytext,myblocks);

but it raise this error:

The class 'RecognisedText' doesn't have a default constructor

I tried to define myobject in different way:

RecognisedText myobject;
myobject.text = mytext;
myobject.blocks = myblocks;

but it raise this error:

'text' can't be used as a setter because it's final.

How i can define my new variable from RecognisedText class and set properties? I can't change any part of class RecognisedText because it is a remote class (added from pubspec.yaml)

Edit: I finally solved my problem with this code:

RecognisedText myobject = RecognisedText.fromMap({"text": mytext, "blocks": myblocks});

CodePudding user response:

Class RecognisedText is designed in the way that you can not create its instance other than using its fromMap constructor, which accepts map.

So the only way to create it is to create map with values you need and pass it to RecognisedText.fromMap constructor.

CodePudding user response:

If you already have your block and your text as variables, you can modify you class like this to accept a default constructor :

class RecognisedText {
  //----Replace this line ----
  //RecognisedText._(this.text, this.blocks);
  //----By this line ----
  RecognisedText.(this.text, this.blocks);

  factory RecognisedText.fromMap(Map<dynamic, dynamic> map) {
    var resText = map["text"];
    var textBlocks = <TextBlock>[];
    for (var block in map["blocks"]) {
      var textBlock = TextBlock.fromMap(block);
      textBlocks.add(textBlock);
    }
    return RecognisedText._(resText, textBlocks);
  }

  ///String containing all the text identified in a image.
  final String text;

  ///All the blocks of text present in image.
  final List<TextBlock> blocks;
}

To complete Alex's answer, the constructor fromMap takes a map as a parameter. It should look like :

RecogniserdText.fromMap({'text': myText, 'blocks': myblock1, 'blocks': myblock2, 'blocks': myblock3});

CodePudding user response:

Your Class can't create its instance so try using YourClassName.fromMap constructor.

  • Related