简体   繁体   中英

Flutter-Dart NoSuchMethodError (NoSuchMethodError: Class 'int' has no instance method 'call'. Receiver: 5 Tried calling: call("25"))

Im using Getx for state management. I'm getting an error when I use getx set method.

I am using this code for Getx class:

import 'package:flutter/material.dart';
  import 'package:get/get.dart';

  // Bu alan benim sayac değişkenlerimi Getx yardımı ile tuttuğum classdır.farkllı değişken classları yazılabilir.
  class SayacController extends GetxController {
    //5 benim değikenimin ilk değeridir.
    var _sayac = 5.obs;

    //get ile çeker, set ile veriyi atarım.
    get sayac => _sayac.value;
    set sayac(yeniDeger) => _sayac.value = yeniDeger;

    //burada 2 tane fonksiyon belirledim. çeşitli fonksiyonlar yazılabilir.
    void arttir() {
      sayac = sayac + 1;
    }

    void azalt() {
      sayac = sayac - 1;
    }
  }

I am using this code for main page.

import 'package:flutter/material.dart';
  import 'package:flutter_ilk_proje/sayac_controllerEx.dart';
  import 'package:get/get.dart';
  import 'package:http/http.dart';

  void main() => runApp(MyApp());

  class MyApp extends StatelessWidget {
    //Burada controller değşkenlerini vs getiriyorum.
    SayacController _controller = Get.put(SayacController());
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Material App',
        home: Scaffold(
          appBar: AppBar(
            title: Text('Material App Bar'),
          ),
          body: Center(
            child: InkWell(
              onTap: () {
                _controller.sayac("25");
              },
              child: Container(
                  child: Obx(
                () => Text(_controller.sayac.toString()),
              )),
            ),
          ),
        ),
      );
    }
  }

and I'am getting this error

     Exception has occurred.
     NoSuchMethodError (NoSuchMethodError: Class 'int' has no instance method 'call'.
     Receiver: 5
     Tried calling: call("25"))

Screenshots about codes.

https://ibb.co/LP85JGR

https://ibb.co/YpFHjQF

https://ibb.co/HqPhxHd

It is really hard to guess what you may have meant.

Should this line: _controller.sayac("25"); maybe be _controller.sayac = 25; ?

Anyway, _controller.sayac("25"); is plain wrong. sayac is a property. You can read it, you can write it, you cannot call it like that.

That said, you seem to try to get by with the bare minimum. Don't do this. Dart is such a wonderful language and it will help you all along the way, if you use it . Get a good linter (did you turn yours off? There should be one by default now...) and let it explain why you need to be verbose and explicit in everything you do and the language will support you as much as possible. This error that you got would be a compile time error if you had used the power of Dart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM