Pheepster
Pheepster

Reputation: 6347

Can't create Color object in Flutter

I am new to Flutter and going through a charts_flutter tutorial. I am trying to create a Color object as defined in the tutorial, however, I am stuck on a compile error. The documentation in the tutorial defines creating a color as follows:

Color(0xFF3366cc) //causes error

However, when I try this, I get the following error when trying to build:

Too many positional arguments: 0 expected, but 1 found.

Code:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:charts_common/common.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

class GaugeLineChart extends StatefulWidget {

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

class _GaugeLineChartState extends State<GaugeLineChart> {

  List<charts.Series<GaugeFlowReading, dynamic>> _flowSeries;

  _getReadingData() {
    List<GaugeFlowReading> flows = [];
    for(int i = 0; i < 100; i++) {
      flows.add(GaugeFlowReading(i * 1000, DateTime.now().subtract(Duration(hours: i)), Color(0xffb74093))); // error on Color object here
    }

    _flowSeries.add(
      charts.Series(
        data: flows,
        domainFn: (GaugeFlowReading reading, _) => reading.timestamp,
        measureFn: (GaugeFlowReading reading, _) => reading.flow,
      )
    );

  }

  Widget build(BuildContext context) {
    return Center(child: Text("*CHART GOES HERE*"));
  }
}

class GaugeFlowReading {
  int flow;
  DateTime timestamp;
  Color color;
  GaugeFlowReading(this.flow, this.timestamp, this.color);
}

I've been unable to find an answer this question so far. Can anyone clarify why this is happening or help out with this?

Upvotes: 0

Views: 281

Answers (2)

EdwynZN
EdwynZN

Reputation: 5601

It seems the package import 'package:charts_common/common.dart'; has its own class Color, you can change it to

import 'package:charts_common/common.dart' hide Color;

or

import 'package:charts_common/common.dart' as charts_common;

so the compiler detects you're indeed using the Color class fo the flutter framework

Upvotes: 1

boringdeveloper
boringdeveloper

Reputation: 446

Tried your code, and the error does exists.

As an alternative you may try this.

Color.fromHex(code: "b74093")

This also seems to be a conflicting problem with the packages you used.

enter image description here

When I commented the import of charts_common, the error you encountered was gone.

enter image description here

Upvotes: 2

Related Questions