0
votes

type 'Null' is not a subtype of type 'String'

Error code with the following code:

class ExpandingItems extends StatefulWidget {
  final product_detail_name;
  final product_detail_price;
  final product_detail_picture;
  final product_detail_description;
  final product_detail_Description_cn;

  ExpandingItems({
    this.product_detail_name,
    this.product_detail_description,
    this.product_detail_picture,
    this.product_detail_price,
    this.product_detail_Description_cn,
  });

  @override
  _ExpandingItemsState createState() => _ExpandingItemsState();
}

class _ExpandingItemsState extends State<ExpandingItems> {
  List<bool> _isExpanded = List.generate(1, (_) => false);

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        child: ExpansionPanelList(
          expansionCallback: (index, isExpanded) => setState(() {
            _isExpanded[index] = !isExpanded;
          }),
          children: [
            for (int i = 0; i < 1; i++)
              ExpansionPanel(
                  body: ListTile(
                    subtitle: Text(widget.product_detail_Description_cn),
                  ),
                  headerBuilder: (_, isExpanded) {
                    return Center(
                      child: Text("Brand"),
                    );
                  },
                  isExpanded: _isExpanded[i]),
          ],
        ),
      ),
    );
  }
}

i've changed the format with .tostring but it will only appear the null text instead of the description i've stored.

Performing hot reload... Syncing files to device Android SDK built for x86...

Exception caught by widgets library

The following _TypeError was thrown building ExpandingItems(dirty, state: _ExpandingItemsState#23c38): type 'Null' is not a subtype of type 'String'

The relevant error-causing widget was:

ExpandingItems file:///C:/Users/nick_/StudioProjects/appsome/lib/pages/Product_Details.dart:224:13

When the exception was thrown, this was the stack:

#0      _ExpandingItemsState.build (package:appsome/pages/Product_Details.dart:266:44)
#1      StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#3      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
...
====================================================================================================
Reloaded 1 of 558 libraries in 2.910ms.
1
Make sure that widget.product_detail_Description_cn has value. - Miftakhul Arzak
@MiftakhulArzak i've applied for alle of my widget a string value. - Linbo Xia
you can check it using print(widget.product_detail_Description_cn) on initState(). And please add log error, so other users can solve it easier. - Miftakhul Arzak
@MiftakhulArzak I've tried both methods but doesn't seems to work - Linbo Xia
No, print(widget.product_detail_Description_cn) on initState() just to see if your variable has value or null, not to solve the error. What is the result of print(widget.product_detail_Description_cn)?. - Miftakhul Arzak

1 Answers

0
votes

Make sure that the widget which is passing data to the ExpandingItems Widget is not passing a nullable type, like String?, but instead a non-nullable type like String.

If this does not help you please provide an updated question which is featuring how ExpandingItems gets its values.


Recommendation:

Use type annotations for variables, e.g. final String myText or final String? myNullableText.

This will help you reducing type-related errors and makes you much more aware about what types you are using / what types you can expect.