skip to Main Content

This is my code:

import 'package:alselre/constants.dart';
import 'package:alselre/size_config.dart';
import 'package:flutter/material.dart';

class Body extends StatefulWidget {
  const Body({super.key});

  @override
  State<Body> createState() => _BodyState();
}

class _BodyState extends State<Body> {
  int currentPage = 0;
  List<Map<String, String>> splashData = [
    {
      "text": "Welcome to alSelre nThe best platform to buy anything!",
      "image": "assets/images/splash_1.png"
    },
    {
      "text": "You'll get fastest delivery with 24/7 customer service.",
      "image": "assets/images/splash_2.png"
    },
    {
      "text": "You'll get the best products nfrom all over the country",
      "image": "assets/images/splash_3.png"
    },
  ];


  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: SizedBox(
        width: double.infinity,
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 3,
              child: PageView.builder(
                itemCount: splashData.length,
                itemBuilder: (context, index) => SplashContent(
                  image: splashData[index]["image"],
                  text: splashData[index]['text'],
                ),
              ),
            ),
            const Expanded(
              flex: 2,
              child: SizedBox(),
            ),
          ],
        ),
      ),
    );
  }
}

class SplashContent extends StatelessWidget {
  const SplashContent({
    Key? key,
    required this.text,
    required this.image,
  }) : super(key: key);
  final String text, image;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Spacer(),
        Text(
          "alSelre",
          style: TextStyle(
            fontSize: getProportionateScreenWidth(36),
            color: kPrimaryColor,
            fontFamily: "Quebecks",
            fontWeight: FontWeight.bold,
          ),
        ),
        Text(
          text,
          textAlign: TextAlign.center,
          style: TextStyle(
            fontFamily: "Muli",
          ),
        ),
        Spacer(
          flex: 2,
        ),
        Image.asset(
          image,
          height: getProportionateScreenHeight(265),
          width: getProportionateScreenWidth(235),
        ),
      ],
    );
  }
}

And I’m having this error:
lib/screens/splash/components/body.dart:39:43: Error: The argument type ‘String?’ can’t be assigned to the parameter type ‘String’ because ‘String?’ is nullable and ‘String’ isn’t.
image: splashData[index]["image"],
^
lib/screens/splash/components/body.dart:40:42: Error: The argument type ‘String?’ can’t be assigned to the parameter type ‘String’ because ‘String?’ is nullable and ‘String’ isn’t.
text: splashData[index][‘text’],
^

And I’m having this error:
lib/screens/splash/components/body.dart:39:43: Error: The argument type ‘String?’ can’t be assigned to the parameter type ‘String’ because ‘String?’ is nullable and ‘String’ isn’t.
image: splashData[index]["image"],
^
lib/screens/splash/components/body.dart:40:42: Error: The argument type ‘String?’ can’t be assigned to the parameter type ‘String’ because ‘String?’ is nullable and ‘String’ isn’t.
text: splashData[index][‘text’],
^

2

Answers


  1. try this code

     itemBuilder: (context, index) => SplashContent(
                      image: "${splashData[index]["image"]}",
                      text: "${splashData[index]['text']}",
                    ),
                  ),
    
    Login or Signup to reply.
  2. The List of Map can be nullable so, you cannot assign that to place where it’s accepting non nullable value.
    You can update your code as below

    SplashContent(
                      image: splashData[index]["image"] ?? 'DEFAULT_IMAGE',
                      text: splashData[index]['text'] ?? 'DEFAULT_TEXT',
                    ),
    

    you can set default text and image. So, when value will be null it will use that default values.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search