Reputation: 517
How do I get access to the _selectedCurrency parameter which inside the following stateful class from another stateful class?
import 'dart:io' show Platform;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'coin_data.dart';
class OsPicker extends StatefulWidget {
@override
_OsPickerState createState() => _OsPickerState();
}
class _OsPickerState extends State<OsPicker> {
String _selectedCurrency = currenciesList[0];
DropdownButton<String> androidDropDown() {
List<DropdownMenuItem<String>> dropdownItems = [];
for (String currency in currenciesList) {
var newItem = DropdownMenuItem(
child: Text(currency),
value: currency,
);
dropdownItems.add(newItem);
}
return DropdownButton<String>(
value: _selectedCurrency,
items: dropdownItems,
onChanged: (value) {
setState(() {
_selectedCurrency = value;
});
},
);
}
CupertinoPicker iOSPicker() {
List<Text> pickerItems = [];
for (String currency in currenciesList) {
var newItem = Text(currency);
pickerItems.add(newItem);
}
return CupertinoPicker(
backgroundColor: Colors.lightBlue,
itemExtent: 32.0,
onSelectedItemChanged: (selectedIndex) {
setState(() {
_selectedCurrency = currenciesList[selectedIndex];
print(_selectedCurrency);
});
},
children: pickerItems);
}
@override
Widget build(BuildContext context) {
return Platform.isIOS ? iOSPicker() : androidDropDown();
}
}
The following is from the coin_dart file:
const List currenciesList = [ 'AUD', 'BRL', 'CAD', 'CNY', 'EUR', 'GBP', 'HKD', 'IDR', 'ILS', 'INR', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'RON', 'RUB', 'SEK', 'SGD', 'USD', 'ZAR' ];
const List cryptoList = [ 'BTC', 'ETH', 'LTC', ];
Upvotes: 1
Views: 92
Reputation: 7308
You can't, the data in Flutter are descending only. A workaround would be to create a global file to store your variables or to use a design pattern like BLoC which allow you to access data from anywhere in your application.
Example of Global
// global.dart
String selectedCurrency;
// Then you can do as follow to access it
import './lib/global.dart' as Globals;
Globals.selectedCurrency = currenciesList[0];
Upvotes: 1