Reputation: 95
I'm practicing a little bit in DartPad some problems and can't get the solution, the code below is what I think is OK, but I'm getting 'Script error' in the console.
The problem that I'm solving is that I need to make function that has two arguments, first one in some random sentence and second is any character, and I need to find how many characters are in that sentence.
Please respond with the error that I don't see or just give me solution. Ty
void main() {
numberOfSameCharacters(randomString: 'Today is a nice day.', character: 'a');
}
void numberOfSameCharacters({String randomString, String character}) {
int sameCharacters = 0;
List<String> randomStringList = randomString.split('');
for (int i = 0; i <= randomStringList.length; i++) {
if (character.toLowerCase() == randomStringList[i].toLowerCase()) {
sameCharacters += 1;
}
}
print(sameCharacters);
}
Upvotes: 0
Views: 469
Reputation: 11
I have the same problem, check this out :
import 'package: flutter/material.dart';
var epicMap = {'Key1': 345, 'key2': 'map example'};
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Mapping with flutter and dart'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key? key, required this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
get key2 => null;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
var _value;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Map output:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
Text(
'ui',
style: Theme.of(context).textTheme.headline5,
),
Slider(
min: 0,
max: 2,
value: _value,
onChanged: (value) {
setState(() {
_value = value;
});
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Upvotes: 1
Reputation: 4894
I think I could give you an idea.
Error is in this line:
for (int i = 0; i <= randomStringList.length; i++)
As we know List / Arrays of size N has index values from 0 to N-1, but this code runs loops for 0 to N. So we get RangeError
Corrected Code:
void main() {
numberOfSameCharacters(randomString: 'Today is a nice day.', character: 'a');
}
void numberOfSameCharacters({String randomString, String character}) {
int sameCharacters = 0;
List<String> randomStringList = randomString.split('');
for (int i = 0; i < randomStringList.length; i++) {
if (character.toLowerCase() == randomStringList[i].toLowerCase()) {
sameCharacters += 1;
}
}
print(sameCharacters);
}
Hope that solves your issue!
Upvotes: 1