[Solved] Flutter how to create package with function


You code is missing a lot of pieces. Just because Dart allows you to write code like a sloppy web developer, does not mean you should. Dart is strongly typed, that is an advantage, please use it.

Problems:

  • ceviri has no explicit type.
  • wiki has no explicit return type.
  • wiki is not awaited
  • Your anonymous method is not async.

More information about Futures, async and await: What is a Future and how do I use it?

Fixing your code as good as possible:

class Sozluk {
  Future<TYPE_X> wiki(TYPE_Y ceviri) async {
    var res = await http.Client()
          .get(Uri.parse('https://sozluk.gov.tr/gts?ara=$ceviri'));
      var body = res.body;
      var decoded = jsonDecode(body);
      var json = decoded[0];
      var sozlukanlam = json["anlamlarListe"][0]["anlam"];
      print(sozlukanlam);
      return sozlukanlam;
  }
}

Test Code:

void main() {
  test('köpek', () async {
    final sozluk = Sozluk();
    var cevap = await sozluk.wiki('köpek');
    print(cevap);
  });
}

Please note that you need to fill in TYPE_X and TYPE_Y, I have no idea which datatypes best represent your data. Is it a number? A text? You decide.

1

solved Flutter how to create package with function