@@ -13,12 +13,16 @@ import 'highlights_plugin_platform_interface.dart';
1313/// An implementation of [HighlightsPluginPlatform] that uses method channels.
1414class MethodChannelHighlightsPlugin extends HighlightsPluginPlatform
1515 implements HighlightsInterface {
16+ MethodChannelHighlightsPlugin ({required this .debug});
17+
1618 /// The method channel used to interact with the native platform.
1719 @visibleForTesting
1820 final methodChannel = const MethodChannel ('highlights_plugin' );
1921
20- List <String >? _languages;
21- List <String >? _themes;
22+ final bool debug;
23+
24+ List <String >? _cachedLanguages;
25+ List <String >? _cachedThemes;
2226
2327 @override
2428 Future <List <CodeHighlight >> getHighlights (
@@ -44,7 +48,7 @@ class MethodChannelHighlightsPlugin extends HighlightsPluginPlatform
4448 if (data is List ) {
4549 return data.map ((e) => CodeHighlight .fromJson (e)).toList ();
4650 } else {
47- print (
51+ _debugPrint (
4852 '${index .getHighlights }: Expected List but got ${data .runtimeType }' ,
4953 );
5054 return [];
@@ -53,16 +57,16 @@ class MethodChannelHighlightsPlugin extends HighlightsPluginPlatform
5357
5458 @override
5559 Future <List <String >> getLanguages () async {
56- if (_languages != null ) return _languages ! ;
60+ if (_cachedLanguages != null ) return _cachedLanguages ! ;
5761
5862 final result = await methodChannel.invokeMethod (index.getLanguages);
5963
6064 if (result is List ) {
6165 final output = result.map ((data) => data.toString ()).toList ();
62- _languages = output;
66+ _cachedLanguages = output;
6367 return output;
6468 } else {
65- print (
69+ _debugPrint (
6670 '${index .getLanguages }: Expected List but got ${result .runtimeType }' ,
6771 );
6872 return [];
@@ -71,31 +75,39 @@ class MethodChannelHighlightsPlugin extends HighlightsPluginPlatform
7175
7276 @override
7377 Future <List <String >> getThemes () async {
74- if (_themes != null ) return _themes ! ;
78+ if (_cachedThemes != null ) return _cachedThemes ! ;
7579
7680 final result = await methodChannel.invokeMethod (index.getThemes);
7781
7882 if (result is List ) {
7983 final output = result.map ((e) => e.toString ()).toList ();
80- _themes = output;
84+ _cachedThemes = output;
8185 return output;
8286 } else {
83- print ('${index .getThemes }: Expected List but got ${result .runtimeType }' );
87+ _debugPrint (
88+ '${index .getThemes }: Expected List but got ${result .runtimeType }' ,
89+ );
8490 return [];
8591 }
8692 }
8793
8894 Future <String > _getLanguage (String expected) async {
89- final data = _languages ?? (await getLanguages ());
95+ final data = _cachedLanguages ?? (await getLanguages ());
9096 final result = data.getMatching (expected);
9197 return result ?? data.first;
9298 }
9399
94100 Future <String > _getTheme (String expected) async {
95- final data = _themes ?? (await getThemes ());
101+ final data = _cachedThemes ?? (await getThemes ());
96102 final result = data.getMatching (expected);
97103 return result ?? data.first;
98104 }
105+
106+ void _debugPrint (String message) {
107+ if (debug) {
108+ print (message);
109+ }
110+ }
99111}
100112
101113extension on List <String > {
0 commit comments