2

I am building a Weather app with Flutter and for this I have to transfer location data from one screen to another screen i.e from splash screen to welcome screen and from welcome screen to main_screen_navigation which has my bottom navigation bar and from this screen to HomeScreen. But the problem is that I have to transfer location data to a constructor. Maybe That's why I am encountering this error. Please help me resolve this one as I have tried all possible answers like using init state. Basically I want to know how to transfer WeatherData from bottom navigation bar screen to another screens. Please answer this question I am searching for answers from 2 days. Thank You!

My main screen_navigation

import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';
import 'home_screen.dart';
import 'search_screen.dart';
import 'saved_screen.dart';
import 'forecast_screen.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';

class MainScreenNavigation extends StatefulWidget {
  MainScreenNavigation({required this.locationWeather});
  final locationWeather;

  @override
  State<MainScreenNavigation> createState() => _MainScreenNavigationState();
}

class _MainScreenNavigationState extends State<MainScreenNavigation> {
  int page_number = 0;
  List screens = [
    HomeScreen(locationWeather: widget.weatherData), //here the error is coming 
    SearchScreen(),
    ForecastScreen(),
    SavedScreen(),
  ];
  @override
  void initState() {
    final weather = widget.locationWeather;
    super.initState();
  }

  final items = [
    Icon(
      Icons.home,
      size: 30,
      color: Colors.white,
    ),
    Icon(Icons.search, size: 30, color: Colors.white),
    Icon(Icons.auto_graph, size: 30, color: Colors.white),
    Icon(Icons.favorite, size: 30, color: Colors.white),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: CurvedNavigationBar(
        backgroundColor: kPrimaryColor,
        color: kPrimaryColor,
        buttonBackgroundColor: kSecondaryColor,
        animationCurve: Curves.fastOutSlowIn,
        height: 60,
        items: items,
        onTap: (index) {
          setState(
            () {
              page_number = index;
            },
          );
        },
      ),
      body: screens[page_number],
    );
  }
}

Home Screen

import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';

class HomeScreen extends StatefulWidget {
  HomeScreen({this.locationWeather});
  final locationWeather;
  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: kPrimaryColor,
    );
  }
}

Welcome Screen

import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';
import 'main_screen_navigation.dart';

class WelcomeScreen extends StatefulWidget {
  WelcomeScreen({required this.locationWeather});
  final locationWeather;

  @override
  State<WelcomeScreen> createState() => _WelcomeScreenState();
}

class _WelcomeScreenState extends State<WelcomeScreen> {
  void getLocationData() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) {
          return MainScreenNavigation(locationWeather: widget.locationWeather);
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: kPrimaryColor,
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(
                height: 120,
              ),
              Image.asset(
                'images/welcome_screen_pic.png',
                height: 250,
              ),
              SizedBox(
                height: 120,
              ),
              Text(
                'Discover the Weather\nin Your City',
                textAlign: TextAlign.center,
                style: kWelcomeScreenTitle,
              ),
              SizedBox(
                height: 20,
              ),
              Text(
                textAlign: TextAlign.center,
                'Get to know your weather maps and\nradar precipitation forecast',
                style: kWelcomeScreenTagLine,
              ),
              SizedBox(
                height: 60,
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: TextButton(
                  onPressed: () {
                    getLocationData();
                  },
                  child: Container(
                    height: 60,
                    child: Center(
                      child: Text(
                        'Get Started',
                        style: kWelcomeScreenButton,
                      ),
                    ),
                    decoration: BoxDecoration(
                        color: kYellowcolor,
                        borderRadius: BorderRadius.all(Radius.circular(15))),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

1 Answers1

1

In mainscreen navigation you initialied weather outside the init state



  List screens = [];
    

  @override
  void initState() {
screens = [
HomeScreen(locationWeather: widget.weatherData), //here the error is coming 
    SearchScreen(),
    ForecastScreen(),
    SavedScreen(),
  ];
    super.initState();
  }
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30