简体   繁体   中英

How can I get variable data into a Class with ChangeNotifier in Flutter?

I have the following class which pulls in data from a remote JSON source, if I hardcode the URL into the class (daUrl) then it works just great.

But I am planning to use a variable URL which I want to feed into this class when I call it. But declaring the variable at the top just doesn't work.

Any ideas?

class ThreadData with ChangeNotifier
{
  Map<String,dynamic> _map = {};
  bool _error = false;
  String _errorMessage = '';
  int _isLoggedIn = 0;
  int tid = 1836392;

  Map<String,dynamic> get map => _map;
  bool get error => _error;
  String get errorMessage => _errorMessage;
  int get isLoggedIn => _isLoggedIn;


  //var daUrl = 'https://jsonplaceholder.typicode.com/todos/';


  Future<void> get fetchData async {

     final response = await post(Uri.parse(daUrl));

    if ((response.statusCode == 200) && (response.body.length >0))
    {
      try
      {
        debugPrint('\Thread => Make Call => Success\n');
        _map = json.decode(response.body);
        _error = false;
      }
      catch(e)
      {
        _error = true;
        _errorMessage = e.toString();
        _map = {};
        _isLoggedIn = 0;
      }
    }
    else
    {
      _error = true;
      _errorMessage = 'Error: It could be your internet connection';
      _map = {};
      _isLoggedIn = 0;
    }

    notifyListeners();  // if good or bad we will notify the listeners either way

  }

  void initialValues()
  {
    _map = {};
    _error = false;
    _errorMessage = '';
    _isLoggedIn = 0;
    notifyListeners();
  }


}

You can pass it to its constructor:

class ThreadData with ChangeNotifier {
  final String url;

  // Defaults to some URL if not passed as parameter
  ThreadData({this.url = 'https://jsonplaceholder.typicode.com/todos/'});

  Map<String,dynamic> _map = {};
  bool _error = false;
  String _errorMessage = '';
  int _isLoggedIn = 0;
  int tid = 1836392;

  Map<String,dynamic> get map => _map;
  bool get error => _error;
  String get errorMessage => _errorMessage;
  int get isLoggedIn => _isLoggedIn;

  Future<void> get fetchData async {
     // Access the URL field here
     final response = await post(Uri.parse(this.url));

    if ((response.statusCode == 200) && (response.body.length >0))
    {
      try
      {
        debugPrint('\Thread => Make Call => Success\n');
        _map = json.decode(response.body);
        _error = false;
      }
      catch(e)
      {
        _error = true;
        _errorMessage = e.toString();
        _map = {};
        _isLoggedIn = 0;
      }
    }
    else
    {
      _error = true;
      _errorMessage = 'Error: It could be your internet connection';
      _map = {};
      _isLoggedIn = 0;
    }

    notifyListeners();  // if good or bad we will notify the listeners either way

  }

  void initialValues()
  {
    _map = {};
    _error = false;
    _errorMessage = '';
    _isLoggedIn = 0;
    notifyListeners();
  }
}

Then you can call it like that:

ThreadData();                              // the URL will be https://jsonplaceholder.typicode.com/todos/
ThreadData(url: 'https://example.com');    // the URL will be https://example.com

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