0

hey guys I'm new to Dart & Flutter and I'm trying to make an app that will track bitcoin price... I'm making a function that gives the user choosing his own currency USD,EUR,EGP ...etc
and I'm using coinapi.io for that

here is my problem : I'm trying to make API's currency can be controlled by the user but it's giving me that error

The instance member 'usercurrncey' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression

Here is my code :

class PriceScreen extends StatefulWidget {
  @override
  _PriceScreenState createState() => _PriceScreenState();
}

class _PriceScreenState extends State<PriceScreen> {
//------------------------------------------------------------------------------
// Var's...
  double? coinPrice;
  var usercurrncey;
  String coinAdress =
      'https://rest.coinapi.io/v1/exchangerate/BTC/$usercurrncey/?apikey=B09F3BE073A348A7B5FB72E720AB2E07';
  String selectedCurrency = 'USD';

//------------------------------------------------------------------------------
// Platform picker...
  Widget getSlectedPlatform() {
    bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
    if (isIOS) {
      return iosPicker();
    } else {
      return androidPicker();
    }
  }

//------------------------------------------------------------------------------
// Android Button...
  DropdownButton<String> androidPicker() {
    List<DropdownMenuItem<String>> dropdownItems = [];
    for (int i = 0; i < currenciesList.length; i++) {
      String currency = (currenciesList[i]);
      var newItem = DropdownMenuItem(
        child: Text(currency),
        value: currency,
      );
      dropdownItems.add(newItem);
    }
    return DropdownButton<String>(
      value: selectedCurrency,
      items: dropdownItems,
      onChanged: (value) {
        setState(
          () {
            selectedCurrency = value!;
            usercurrncey = selectedCurrency;
            print(value);
          },
        );
      },
    );
  }

//------------------------------------------------------------------------------
// IOS BUTTON...
  CupertinoPicker iosPicker() {
    List<Text> CupertinoList = [];

    for (var Currency in currenciesList) {
      CupertinoList.add(Text(Currency));
    }
    return CupertinoPicker(
      itemExtent: 32.0,
      onSelectedItemChanged: (selectedIndex) {
        print(selectedIndex);
      },
      children: CupertinoList,
    );
  }

//------------------------------------------------------------------------------
// prics function...
  void getData() async {
    http.Response response = await http.get(Uri.parse("$coinAdress"));
    var data = response.body;
    var Price = jsonDecode(data);

    var time = Price['time'];
    var currencyValue = Price['rate'];
    var currentCurrncy = Price['asset_id_quote'];

    print('Currnt Currency is : $currentCurrncy');

    print('time is :$time');
    print('currencyValue is : $currencyValue');
    coinPrice = currencyValue;
  }

//------------------------------------------------------------------------------
// Code Start...
  @override
  Widget build(BuildContext context) {
    getData();
    return Scaffold(
      appBar: AppBar(
        title: Text(' Coin Ticker'),
      ),
//-----------------------------------------------
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
            child: Card(
              color: Colors.lightBlueAccent,
              elevation: 5.0,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0),
                child: Text(
                  '1 BTC = $coinPrice USD',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
//-----------------------------------------------
          Container(
            height: 150.0,
            alignment: Alignment.center,
            padding: EdgeInsets.only(bottom: 30.0),
            color: Colors.lightBlue,
            child: getSlectedPlatform(),
          ),
        ],
      ),
    );
  }
}
AHMED Gamal
  • 37
  • 1
  • 5

0 Answers0