Search code examples
flutterdartflutter-provider

Provider is notifying listeners but not sending data


As the title says, the provider is notifying the listeners but not sending the data. How do i know it is notifying ? Because debugging breakpoints in the screen do activate after the notifyListeners(). The weatherOfToday in the provider is receiving the data from the call, but somehow it get stuck there and doesn't get to the screen.

this is the provider>

import 'package:flutter/material.dart';
import 'package:weather_app/exthernal/models/first_call_model.dart';
import 'package:weather_app/exthernal/models/w_response_model.dart';
import 'package:weather_app/exthernal/w_service.dart';

class WeatherProvider extends ChangeNotifier {
  WeatherService firtCaller = WeatherService();
  WeatherService weatherCall = WeatherService();
  FirstCallModel firstCall = FirstCallModel();
  WeatherResponseModel weatherOfToday = WeatherResponseModel();

  weatherCaller() async {
    var firstCall = await firtCaller.getFirstCall();
    var weatherOfToday = await weatherCall.getHttp();
    notifyListeners();
  }
}

this is the screen:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:weather_app/logic/w_provider.dart';

class WeatherToday extends StatelessWidget {
  const WeatherToday({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (context) => WeatherProvider(),
        child: Consumer<WeatherProvider>(
            builder: (context, value, child) => Scaffold(
                  body: Center(
                    child: Column(
                      children: [
                        ElevatedButton(
                            onPressed: value.weatherCaller,
                            child: const Text('call')),
                        Text(value.weatherOfToday.nombre ?? 'empty')
                      ],
                    ),
                  ),
                )));
  }
}

let me know if i omitted any key information.

Thanks !


Solution

  • weatherOfToday is not changed when weatherCall.getHttp() is called, instead the local variable is assigned.

    WeatherResponseModel weatherOfToday = WeatherResponseModel();
    
    weatherCaller() async {
      // var weatherOfToday = await weatherCall.getHttp();
      weatherOfToday = await weatherCall.getHttp();
      notifyListeners();
    }