0
votes

I am passing in name object as a STRING into a Stateless Widget to define logic functions. When passing in the String I get the error Expected a value of type 'WIDGETNAME', but got one of type 'String'.

How do change the Stateless widget to receive the String?

WIDGET

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:oblio_app/ui/theme/colors.dart';
import 'package:oblio_app/ui/widgets/list_builders/list_builder_attribution.dart';

class NestedTabsAttributionCard extends StatefulWidget {
  @override
  _NestedTabsAttributionCardState createState() =>
      _NestedTabsAttributionCardState();
}

class _NestedTabsAttributionCardState extends State<NestedTabsAttributionCard>
    with TickerProviderStateMixin {
  TabController _nestedTabController;

  @override
  void initState() {
    super.initState();

    _nestedTabController = new TabController(length: 3, vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _nestedTabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //  double screenHeight = MediaQuery.of(context).size.height;
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        Container(
          alignment: Alignment.centerLeft,
          margin: EdgeInsets.fromLTRB(0, 0, 0, 00),
          child: TabBar(
            controller: _nestedTabController,
            indicatorColor: CompanyColors.indigo[400],
            labelColor: CompanyColors.indigo[400],
            unselectedLabelColor: CompanyColors.font_primary[60],
            isScrollable: false,
            tabs: <Widget>[
              Tab(
                text: "Campaign",
              ),
              Tab(
                text: "Persona",
              ),
              Tab(
                text: "Creative",
              ),
            ],
          ),
        ),
        Flexible(
          flex: 1,
          child: Container(
            //   height: screenHeight * 1.00,
            alignment: Alignment.center,
            child: TabBarView(
              controller: _nestedTabController,
              children: <Widget>[
                ListBuilderAttribution(name: 'Campaign'),
                ListBuilderAttribution(name: 'Persona'),
                ListBuilderAttribution(name: 'Creative'),
              ],
            ),
          ),
        )
      ],
    );
  }
}

LISTBUILD WIDGET

import 'package:flutter/material.dart';
import 'package:oblio_app/ui/layouts/list_tiles/columns/attribution/attribution.dart';
import 'package:oblio_app/ui/theme/colors.dart';
import 'package:oblio_app/ui/widgets/dividers/attribution_vertical_divider.dart';

class ListBuilderAttribution extends StatefulWidget {
  ListBuilderAttribution({
    Key key,
    this.name,
  }) : super(key: key);

  final String name;

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

class _ListBuilderAttributionState extends State<ListBuilderAttribution> {
  List<LayoutListTileColumnAttribution> attribution = [
    LayoutListTileColumnAttribution(
      progress: '30',
      field1: 'Goal',
      field2: 'Persona',
      field3: 'UseCase',
      field4: 'AdGroup',
    ),
    LayoutListTileColumnAttribution(
      progress: '20',
      field1: 'Goal',
      field2: 'Persona',
      field3: 'UseCase',
      field4: 'AdGroup',
    ),
    LayoutListTileColumnAttribution(
      progress: '10',
      field1: 'Goal',
      field2: 'Persona',
      field3: 'UseCase',
      field4: 'Ad Group',
    ),
  ];

  var label1;
  var label2;
  var label3;
  var label4;

  var ringColor;

  @override
  void initState() {
    super.initState();
    if (widget.name == 'Campaign') {
      label1 = 'Goal';
      label2 = 'Persona';
      label3 = 'Use Case';
      label4 = 'Ad Group';
    } else if (widget.name == 'Persona') {
      label1 = 'Goal';
      label2 = 'Persona';
      label3 = 'Use Case';
      label4 = 'Ad Group';
    } else if (widget.name == 'Creative') {
      label1 = 'Goal';
      label2 = 'Persona';
      label3 = 'Use Case';
      label4 = 'Ad Group';
    } else {
      label1 = 'Error';
      label2 = 'Error';
      label3 = 'Error ';
      label4 = 'Error';
    }
    if (attribution.indexOf(label1) == 0) {
      ringColor = CompanyColors.indigo[500];
    } else if (attribution.indexOf(label1) == 1) {
      ringColor = CompanyColors.orange[500];
    } else if (attribution.indexOf(label1) == 2) {
      ringColor = CompanyColors.red[500];
    } else if (attribution.indexOf(label1) == 3) {
      ringColor = CompanyColors.green[500];
    } else {
      ringColor = CompanyColors.red[900];
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
        separatorBuilder: (context, index) => AttributionDivider(),
        scrollDirection: Axis.horizontal,
        shrinkWrap: true,
        itemCount: 3,
        itemBuilder: (context, index) {
          return LayoutListTileColumnAttribution(
            ringColor: ringColor,
            progress: attribution[index].progress,
            label1: label1,
            field1: attribution[index].field1,
            label2: label2,
            field2: attribution[index].field2,
            label3: label3,
            field3: attribution[index].field3,
            label4: label4,
            field4: attribution[index].field4,
          );
        });
  }
}

The following TypeErrorImpl was thrown building KeyedSubtree-[<0>]: Expected a value of type 'LayoutListTileColumnAttribution', but got one of type 'String' The relevant error-causing widget was: TabBarView file:///Users/timothysolomon/Development/apps/oblio_app/oblio_app/lib/ui/widgets/tabs/attribution_card/nested_tabs_attribution_card.dart:65:20 When the exception was thrown, this was the stack: dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 195:49 throw_ dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 44:3 castError dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 406:10 cast dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/classes.dart 541:14 as_C packages/oblio_app/ui/widgets/list_builders/list_builder_attribution.dart 74:37 initState ...

2

2 Answers

2
votes

Problem is definitely in following line.

if (attribution.indexOf(label1) == 0)

Here, you are trying to search(compare) label1 in attribution, which is LayoutListTileColumnAttribution type list, so to compare you have to pass LayoutListTileColumnAttribution type object in indexOf.

To compare class object you have to override = method and also hashmap but better option is to go with equatable package, so you don't need to create lots of boilerplates code.

Finally, here comes to actual problem.

Here you want to compare string with object's variable which is not possible directly.

I think you have to do using iterating through whole list and compare with label1.

 int findindex = -1;
  int i;

for (i = 0; i < attribution.length; i++) {
      if (attribution[i].field1 == label1) {
        findindex = i;
        break;
      }
    }
    if (findindex == 0) {
      ringColor = CompanyColors.indigo[500];
    } else if (findindex == 1) {
      ringColor = CompanyColors.orange[500];
    } else if (findindex == 2) {
      ringColor = CompanyColors.red[500];
    } else if (findindex == 3) {
      ringColor = CompanyColors.green[500];
    } else {
      ringColor = CompanyColors.red[900];
    }
0
votes

Super simple!

Create a method in the listbuilder for getRingColor

   Color getRingColor(int index) {
    return [
      CompanyColors.indigo[400],
      CompanyColors.orange[400],
      CompanyColors.red[400]
    ][index];
  }

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
        separatorBuilder: (context, index) => AttributionDivider(),
        scrollDirection: Axis.horizontal,
        shrinkWrap: true,
        itemCount: 3,
        itemBuilder: (context, index) {
          return LayoutListTileColumnAttribution(
            ringColor: getRingColor(index),
            progress: attribution[index].progress,
            label1: label1,
            field1: attribution[index].field1,
            label2: label2,
            field2: attribution[index].field2,
            label3: label3,
            field3: attribution[index].field3,
            label4: label4,
            field4: attribution[index].field4,
          );
        });
  }
}